What is a Data type?
In each application we create we will use data such as numbers or text .
In the C# Application you have to determine the type of each piece of data you dealing with and that makes the application know what can you do with the data or what you can not do with it .
for example if you have the number 5 you must tell the program what kind of 5 that you want to deal with ,you must tell the application if it is an integer like a number of students for example or it just a number of a street in an address text ,so the application determine the types of operations that can be done on it and the others that can't be done ,so if the number 5 means an integer that means you can add it to another integer or subtract it from another or multiply it by another or divide it by another, but if you mean the number 5 as a text that means that you can't do any of the previous operation on it.
Beside the types of operations that the program can determine from the type of data also the program determine the amount of space needed in memory to store the data based on its type ,meaning the space needed to store the number 5 as integer in 32 bytes but space needed to store the number 5 as text is more than that ,so the right type makes the application work write.
Most Commonly used Data types in C#
int : ( integer ) used for numbers without floating point.
double: used for numbers with floating point.
string : used for text.
char:one character.
bool:used to store one value of two ( true , false ) .
Variables
What is a Variable?
A Variable is space in memory (RAM) that can we use to store a piece of data ,we give that space a name so it can be easily reached any time later .
How to Declare a Variable in C#
Declaring a Variable is so easy ,you just determine the data type of the data that will be store int the variable than give the variable a name ,see the picture bellow:
As you can see in the picture above we declared an integer variable using the keyword 'int' which refer to the integer data type ,after the type we have a space and then the name of the variable which we can freely choose it then comes the semicolon ';' to tell the application that the statement ends here.
Notice that under the Variable name there are a green line that tells there is something wrong ,do not worry ,the visual studio tells you that the Variable that you declared has no data yet.
Assign a value to the variable
Once you declared the Variable you can assign a value to it by calling its name followed by equal sign ' = ' followed by the value that you want to store ,see the picture bellow :
You also can Declare the Variable and assign a value to it in one step ,see the picture below:
Now we can make some Variables of different data types ,see the picture below:
The previous picture shows the different types of data types and how each type take its value .
Notice the 'string' variable takes the value surrounded by double quotes and the 'char' variable surrounded by single quotes.
Variable Naming rules
When you declare a Variable you must consider the following :
- Variable name must not be a C# reserved keyword :the C# language has reserved keywords that can not be used as variable name ,these keywords usually takes the blue color like 'int,double,string,char,bool,namespace,class,static,void,public' etc,follow this link to find a complete list of C# reserved keywords .
- You can not use spaces in the Variable name ,meaning if you want to give a name to a Variable that contain more than one word do not use spaces but you can use underscore instead,or you can give a name to your Variable using 'CamelCase' technique ,meaning that you can write the variable name without spaces starting each word with upper case letter such as 'MyFirstLetter' , follow the following link to read more about Camel Case .
- Variable name can not start with a number ,you can use numbers any where in the Variable name but not the beginning of the name, the Variable name must be a letter or Underscore .
Advertising