Data Types and Variables
Defining function and uses of basic data types and variables
When developing an application, we work with different types of values such as a name entered by the user, the result of a calculation or a date. The application needs to store these values in the computer's memory so that it can process them.
Let us understand this using a simple example. We want to create an application that allows the user to enter two numbers and display their sum. When the user enters the two numbers, our application needs to store it in computer's memory before it can calculate the sum. When the sum is calculated, the result is again stored in the computer's memory before it is displayed to the user.
But how is our application going to store the numbers in the computer's memory? This is where the variables help us. Using variables, we can provide names to the computer's memory locations. For example, we can specify that the first number will be stored in a memory location whose name is `Number1'. Similarly, the second number will be stored in a memory location with the name `Number2'. These names are easy for us to remember and our application will use these names for further calculations. We do not need to know where exactly the memory locations are situated in our computer. All we need to know is that their names are `Number1' and `Number2'.
Hence, whenever we want to store values in our application, we need to use variables. However, there is one more factor. We also need to specify what kind of value will be stored in a variable.
In the above example, we talked about two variables to store two numbers. But what if the user enters two names in place of two numbers? Naturally, our program will not be able to produce the sum. That is why when we use variables, we also need to specify the type of data that is going to be stored in them. This is done using Data Types. A data type specifies whether a variable can store a number, a single character or text.
Variables are names given to the memory location for storing data.
Data Types are used to specify the types of values associated with variables.
Some commonly used data types in C# are:
|
Data type |
Use |
|
int |
For storing integer values |
|
long |
For storing large integer values |
|
float |
For storing floating point values |
|
double |
For storing large floating point values |
|
char |
For storing a single character |
|
string |
For storing a string |
|
bool |
For storing the values `true' or `false' |
Before we can use a variable in our program, we need to declare it. The syntax for variable declaration is:
data-type variable-name;
For example, the following statement declares an integer variable named `salary':
int salary;
We can declare more than one variable in a single statement if they are of the same data type:
int number1, number2;
However, if the variables are of different data types, they should be declared in separate statements:
string name; int age;
Note: A variable name must start with a letter, the @ symbol or the underscore symbol. However, it is not recommended to name a variable with an underscore symbol as the first character. Also, we cannot use a keyword as a variable name. Remember that C# is a case sensitive language so `age', `Age' and `AGE' are three different entities.
Some examples of proper variable names:
- myAge
- Name
- Sale_Value
- number1
- _hello
Some incorrect variable names:
- 1number // starts with a digit
- my name // has a space between `my' and `name'.
- for // `for' is a keyword
After we declare a variable, we can assign value in it:
int number; number=26;
After the second statement, the value 26 is stored in the variable `number'. Note that the variables can change their values during the program. Hence, if we write a statement like:
number=4;
the variable `number', which earlier has the value 26, will now have the value 4 stored in it.