we are gonna start our journey by learning about variables and constants let's start by two definitions a variable is a name that we give to a storage location in memory where we can store a value constant in an immutable value that is a value that we know at compile time and that value cannot change throughout the life of the application why do we use constants the reason for that is to create safety in our application imagine you're creating an application that involves some mathematical computation around circles we have this number called P which is 3.14 and we use that to calculate the area of a circle that number should always be the same we don't want to accidentally change that in our program if you accidently change that everything is going to blow up in terms of the results the program produces so we declare that number as a constant and this way we create safety in our application in c-sharp to declare a variable we start with the type followed by an identifier and finally semicolon here ain't represent integer which is a number between minus two billion and plus two billion later in this video I will show you all the primitive types that you need to know also note that su sharp is a case sensitive language which means in this case these two identifiers are different one has lowercase n and 1 has uppercase n when declaring a variable we can optionally assign it a value like what you see here don't have to but there is one thing you need to know about variables and that is you cannot use a variable unless you initialize it so let's say if I declare this int number here and I decide to display it on the console my application will not become part I have to assign it a value before I can use it before I can read it I will show you that later when we jump to coding to declare constant we start with the keyword Const next we have the datatype and an identifier and here we have to initialize it with some value we cannot define a constant without setting its value there are a few things you need to know about identifiers first one is that an identifier cannot start with a number so you cannot have an identifier like the one you see here instead you need to replace one with the word one also an identifier cannot include whitespace so you cannot have an identifier like first space name it has to be one word an identifier it cannot be a reserved keyword like int that you saw earlier if you're desperate to use a word that coincidentally clashes with one of the c-sharp keywords you can perfect that with the @ sign and finally as a recommendation always use meaningful names for example avoid a name like f n instead use first thing this way your code will be more readable or maintainable and cleaner and everybody will understand in terms of naming convention here are three popular naming conventions that have been around in the C language family we've got camelcase Pascal Ches and Hungarian notation camelcase as you see I have bolded here the first letter of the first word is lowercase and the first letter of every word after has to be uppercase with Pascal case the first letter of every word has to be uppercase with Hungarian notation we prefix the name of a variable with the datatype it uses so here str represents a string Hungarian notation is not used in c-sharp and I have noticed programmers coming from C or C++ background use that in their code if you're one of them I highly recommend you not to use Hungarian notation because c-sharp developers are not used to that and they don't like to see Hungarian notation in the code it makes your code look a little bit ugly so in c-sharp to name your local variables use camel case so as you see here the first letter of the first word is lowercase and if we had more words here in the identifier the first letter of every word had to be uppercase for constant use Pascal case so here you see I've got a constant here constant integer and the first letter of every word is uppercase here is the list of most commonly used primitive types in c-sharp in fact the actual list is slightly bigger than this but I deliberately decided not to include those data types because they're hardly ever used in fact they are there for interoperability with other languages I personally over the past 12 years of me coding in c-sharp I've hardly ever used them in fact never so I decided not to confuse you with too much details that you don't need in the next slide I've got a link here in case you want to learn about the other primitive types so let's take a look at the table here on the left side I have divided these datatypes into four categories integral numbers real numbers character and boolean this column shows the C sharp data times and these are C sharp keywords note that C sharp keywords are always lowercase each of these C sharp keywords or C sharp types maps to a type in dotnet framework which is displayed in this column so these types are part of the.net framework and when you compile your application the compiler internally would translate the c-sharp key word you use here two equivalent dotnet type the third column here shows the number of bytes each data type uses and I have listed these data types from the smallest to the largest in each category so in the category of integral numbers you see byte is the smallest it takes only one byte whereas long is the largest and it takes eight bytes the more bites we have the more storage we have and we can store larger numbers you don't really have to memorize the range of each data type but remember white can store a value between 0 to 255 short can store a value between minus 32,000 to plus 32,000 integer can store a value between minus 2 billion to Plus 2 billion and long is even bigger than that in terms of real numbers we have three data types float double and decimal float maps to the single type in that framework and it takes four bytes and as you see it can store a very large number double is twice as big so it uses eight bytes and decimal uses sixteen bytes the more precision you need the bigger datatype use we also have character which is represented by char keyworth and it's two bytes so characters in c-sharp are unicode and finally we have bull which represents boolean which can be either true or false in case you want to learn more about the other data types that I told you they're not really used you can simply go to Google and search for c-sharp built-in types and the first page is the MSDN page that lists all the primitive types in c-sharp and most of these data types are pretty straightforward but there is something tricky about real numbers in this table I have listed the data types we have for real numbers load double and decimal I've highlighted double because that's the default data type used by a c-sharp compiler when you're using real numbers so if you want to declare a float you need to explicitly tell the compiler to treat the number you have as a float here is an example I've declared a float called number as I need one point two here I have added the suffix F and that is to tell the compiler to trade this number as a float if I didn't have this F here compiler would think one point two is a double because double is the default data type for real numbers and of course I cannot assign a double number into a float so the program would not compile same applies to decimals so if you want to declare a decimal you need to add the suffix M at the end of the number in c-sharp we also have a few other times which are not considered primitive types and they are string array enum and class you'll learn more about them throughout this course okay that's it for this lecture in the next lecture we're going to talk about the concept of overflowing I hope you  enjoyed this lecture and thank you 

Post a Comment

If you have any doubt please let me know

Previous Post Next Post