As we said before that computer programs work on data so we need to know how we can store data and how we can use this data in our program's code to do our tasks.
To understand how we can store and manipulate data in C# let's take an example of storing 2 integer numbers (integer numbers are the whole numbers like 1,2,3 and 1000 as we will know later).
The computer and the compiler of C# are responsible for allocating memory locations for program's data. For example using C# coding you can tell the compiler to create a location named x that will store integer value also you can tell it to allocate another location called y that will store another number. Figure 2.1 displays our memory after this operation.
Figure 2.1
Now the computer memory has 2 memory locations named x and y and as you can see there are enough room for more data. Now let's assume that we set values (numbers) for these locations: x will store 200 and y will store 34 so the next figure 2.2 will displays our memory after this operation.
Figure 2.2
Now this is our memory so suppose that our program contains a normal add operation using the normal math add operator "+" so we will need a third location that will store the total value of (x + y) so let's say we will call it z. Figure 2.3 displays this adding operation.
Figure 2.3
This is was just a simple discussion about storing whole numbers into our memory. What worth noting here is that each of these locations has a name (x, y and z) and each of them know exactly that it will store whole numbers (integers) and not another type of data so if you try to store a text like "Hello" in these locations the compiler will generate an error and you will not be able to perform the adding operation.
Also note that you can change the value stored in these locations. For example, here in our little program x = 200 but for some reason you need to change its value to 2 only so the new value will replace the old one (200) and when you perform the adding operation again you will get a new value in the location z.
Now after this very simple introduction let's discuss how we can create these memory locations and how we can specify the type of the stored data.
Variables
Well, a variable is just a memory location that can store data that we can use in our code to perform many kinds of operations like (arithmetic operations, comparison operations and many others). Recall from the last few sections that the memory locations x, y and z are variables and if you note that we have done one arithmetic operation there (we add the values stored in x and y to the variable z). They call it a variable because the value stored in a specific memory location can change (a new value replace the old value) so the expression variable came from the fact that the value stored in the memory location for that variable can be changed.
important note: The value of the variable will not change itself but you will maybe need to change it using code.
One important concept you must grasp here is that each variable must store data of a specific type and that will move us to discuss data types.
Data Types
Data can take many different formats (when storing it in our variables) like whole numbers, floating point numbers, text data, images, and many others. We can divide data types into 2 major categories:
1. Predefined data types (Built-in data types).
2. User defined data types (which we will create in the second part of the book).
The predefined data types built-in inside C# and we will use it whenever we write code and that's why C# design team create them for us because we will use it every time we will create a C# program so they built it in the language. So how we will use these types? The answer is very simple: when defining a variable the C# compiler needs to know which data type you want use for this variable.
Let's take an example of creating and storing data inside 2 variables and after we finish you will understand why it's important to C# compiler to know the type of the data that a variable can store: Suppose we have a variable x and variable y now C# compiler don' know which data type that these variables can store now imagine that x store 3 and y store the text "Alan". Now your program needs to add these variables and store the result inside the variable z. so how C# compiler will add 3 to "Alan"? How can you add a number to a text (3 + Alan)? It's impossible. That's why C# compiler must know the type of data that can be stored in a given variable.
The next sections will introduce the built-in types and a description for each type.
The short data type
short data type store integer numbers (whole numbers) values between the number -32,768 and the number 32,767. In other words, if you have a variable that need to store a number between these 2 ranges then use the short data type so the compiler will understand and allocate the necessary memory space for that type. In fact C# compiler allocate 16 bit for that type. If you try to store a value that exceed the maximum value of the short data type (which is 32,767) then the compiler will generate an error.
The ushort data type
ushort data type store integer numbers too but the range of numbers that can be stored will vary from the short data type. ushort store numbers from 0 to 65,535 and you use it when you need a variable that store only positive numbers from 0 to the maximum range of that time (which 65,535). Also C# will allocate the same 16 bit in memory like the short data type. One of my students asked me the following question:
"Why the compiler allocate 16 bit for both data types (short and ushort) although that the maximum range for ushort is almost the double of the maximum range of the short data type?"
My answer was very simple:
"Look at the minimum range for both types, short data type store from -32,768 to 32,767 which are almost the same for ushort (both will count around 65,535 but short will begin with a negative value of -32,768 to 32,767 and ushort will begin from 0 to 65,535). So if you set back and think about it for a minute you will note that the possible values for both types equal to around 65,535 but the minimum or the beginning range is different for both types. So we have the choice to select between those types and that's depending on our needs. If we need a variable that will store a positive number less the maximum range for the ushort type then we use it and if we need a variable that will store a negative or positive values so short data type will be used.
The int data type
int data type store integer numbers (whole numbers) values between the number -2,147,483,648 and the number 2,147,483,647. C# compiler allocate 32 bit for that type. If you try to store a value that exceed the maximum value of the int data type (which is 2,147,483,647) then the compiler will generate an error.
The uint data type
uint data type store integer numbers (whole numbers) values between the number 0 and the number 4,294,967,295. C# compiler allocate 32 bit for that type. If you try to store a value that exceed the maximum value of the uint data type (which is 4,294,967,295) then the compiler will generate an error. But if you need a wider range of values use the next 2 data types.
The long data type
If the previous data types ranges will not satisfy your needs then you can use the long data type. long data type store numbers (whole numbers) values between the number -9,223,372,036,854,775,808 and the number 9,223,372,036,854,775,807. As you might guess the compiler will allocate more space than the previous types so it will allocate 64 bit for that type.
The ulong data type
I think know you can guess what the "u" letter is for; it's stand for unsigned so that type will not allow a negative values for its range. ulong data type store numbers (whole numbers) values between the number 0 and the number 18,446,744,073,709,551,615 and the same thing as long data type the compiler will allocate 64 bit too for that type.
There is another data type for integral number which can store numbers from 0 to 255 let's go and meet him.
The byte data type
byte data type store numbers from 0 to 255 only and C# compiler will allocate only 8 bit space for that type. Why we will need this very small type? Maybe you need to store or count the number of your class's student which will not exceed 10 or 20 at most.
The sbyte data type
sbyte data type store numbers from -128 to 127 only and C# compiler will allocate only 8 bit space for that type.
These was the data types which are built-in C# for integral numbers but what if you need to store floating point numbers like 1.4, 432.432 and 89383.03939292 C# designers didn't forget that and they created 3 data types for storing floating point numbers. Let's check them
The float data type
float data type is the first of 3 data types that can store floating point numbers and actually this is the smallest of the 3 types. float variables store floating point numbers from 1.5 times 10 to the 45 th (1.5 X 10-45 ) to 3.4 times 10 to the 38 th (3.4 X 1038 ) in 32 bit. And this type has a precision to 7 numbers.
The double data type
double data type variables can store floating point numbers from 5 times 10 to the 324 th (5.0 X 10-324 ) to 1.7 times 10 the 308 th (1.7 X 10308 ) in 64-bit. And this type has a precision to 15-16 numbers.
The decimal data type
decimal data type variables can store 1 times 10 to the 28th 1.0 X 10-28 and 7.9 times 10 to the 28 th 7.9 X 1028 in 128 bit. If you think about this type you will note that it has a greater precision and a smaller range and that's we exactly needs for financial calculations also note that decimal data type has a Precision of 28-29 significant digits.
Why we have 3 floating point data types?
To answer this question I will write the simple next C# application:
using
System;
namespace ConsoleApplication2
{
class Class1
{
static void Main(string[] args)
{
float x = 1232.34217F * 1232.34217F;
double y = 1232.34217D * 1232.34217D;
decimal z = 1232.34217M * 1232.34217M;
Console.WriteLine("The float variable x = {0}", x);
Console.WriteLine("The float variable y = {0}", y);
Console.WriteLine("The float variable z = {0}", z);
Console.ReadLine();
}
}
}
Now create a new console application project and then replace the VS. NET auto-generated code with the code above then run the application. You will get the next result from the program output:
The float variable x = 1518667
The double variable y = 1518667.22396031
The decimal variable z = 1518667.2239603089
This is what we have done there:
1. We have created 3 variables of float, double, decimal data types respectively.
2. Then we assign a value to those variables (the value of those variables is the result of the multiplication operation of 1232.34217 * 1232.34217)
3. Then we output the 3 variable's values to the console.
You may think that the value of the 3 variables must be the same because they multiply the same numbers. But this is wrong because we used 3 different data types for these variables. Look again to the float, double, decimal data types and you will note that there is a precision for all of them for example; the float data type variable can be precise to 7 digits only, double to 15-16 and decimal to 28-29 and that's exactly what happened here if you count the digit in each output number you will notice that the first is 7, the second is 15 and the third one which is a decimal variable is 17 digits. So we use only the type that will be useful for us.
important note: We didn't discuss how to write or read C# code so don't worry if you didn't understand the code above. Also note the suffixes (F, D, M) in the numbers above they are useful to the compiler just to tell him that 1.5F is float and 1.5D is decimal. We will discuss it later in the article.
These was the built-in types for integer and floating point numbers but there are more 3 built-in data types that we need to discuss here.
The bool data type
bool data type variables will seem strange concept to non-programmers because of its possible values. There are only 2 possible values to this type true and false. true and false are C# Keywords so when you need a variable of bool data type you must assign it true or false only. You may ask yourself what's the reason of storing only 2 values. In your code you will need to test for many conditions and you will find that this data type variables is the only solution; for example, you need to create a program that will ask the user a question and if the user answer it then the program will ask him another one and so on, if you use a bool variable then you can set the value to true if the user answer the question and to false if the user didn't. Now you can test for this variable's value and if it's true (the user answer the question) then you write the code that will display the next question and if the variable's value is false (the user didn't answer the question) then you will end with program or something else. I mean that it's your responsibility to determine when to use bool variables or other data type variables.
The string data type
In fact we call that type a character string too because this type store characters. string variables store a string of characters like "Michael", "Alan" and "Hello guys, it's me". This type have a special manipulation as we will know later in the book but for now just understand that this type store character string or in other words store text. When you store a string value into your variable delimit it with the double quotation like this: "This is a string".
The char data type
This data type can store characters for most of the known written languages throughout the world. For example you can store the English character 'A' into a char variable or any other character. So char variables can store any character found in your keyboard even the space character or the tab character. When you store a char value into your variable delimit it with a single quote like this: 's' and to store the space character you can type the a single quote then take a space and close it like this: ' ' and the same with the tab character: ' '. But there is more intuitive way to do so as we will know later. C# compiler will allocate 16 bit for char variables.
Still there is another built-in data type called object but I will not mention it here and I will wait until the second part of the book. After data-types section there is an important question.
Why there are many data types for numbers?
Many of you will ask the same question "Why we have many types for storing numbers instead of having only one type to store any number?" The answer is related to memory management. Imagine that we have only one data type for storing any number (integers, floating point numbers) and this data type will allocate for example 256 bit for its value so if you need to store 20 numbers for your program use you will need 20 variables of that data type (20 x 256 = 5120 bits). Now imagine that we have 2000 variables in complex application of that data-type that can store any number we will need (512,000 bits). It's a mass and memory waste.
C# design team address the memory problem and actually they create several data-types with different ranges for storing numbers (byte, sbyte, short, ushort, int, uint, long, ulong, single, double, decimal as we already know). Each type will store a range of numbers for example byte data type will store numbers from 0 to 255 only and it will use only 8 bits of the memory space, the same thing apply to all of the numeric data types they will store a range of numbers and they will allocate only the necessary space for that range so that's how we can save space. To understand that let's look at the next example:
We have an application that needs 3 variables to store numbers so if we have only one data type for storing numbers that will allocate 256 bits for its value then our variables will be like following:
x store the number 20 and there are 256 bits for this variable
y store the number 2000 and there are 256 bits for this variable
z store the number 43546 and there are 256 bits for this variable
So we have 3 variables that allocate (256 x 3) 768 bits of the memory. If you think about it for a minute you will understand that having many data types for storing numbers will be better solution as following:
Now let's use C# data-types to store these numbers into variables (x, y, and z). If you look at the numbers (20, 2000, and 43546) you will notice that we can use (byte, short, and ushort respectively) to store these numbers.
x is a variable of type byte that store the number 20 in only 8 bits
y is a variable of type short that store the number 2000 in only 16 bits
z is a variable of type ushort that store the number 43546 in only 16 bits
Now we have 3 variables that allocate (8 + 16 + 16) 40 bits for its values. So we store the same numbers in 40 bits instead of 768 bits. So instead of having only one big data-type for storing numbers we have different types that can be used to store various ranges of numbers and take up different amounts of memory (8, 16, 32, 64, 128 bits depend on the data type).
Now I think you are ready to know how we can create variables and store values inside them.
Declaring your variables
Until now we discussed what a variable is but we didn't discuss how we can create variables for our programs and how we can use them. In this section we will learn how to do this.
To create a variable in your program you must declare it. Declaring your variable tells the C# compiler information about that variable like the following:
1- What's the data type of that variable?
2- What's the name of that variable?
3- Any other operation that C# compiler will perform behind the scenes.
You can declare a variable by writing its type followed by a space (Whitespace) then its name (identifier) then end the statement with a semicolon. Examples of declaration:
int
x;
short y;
decimal u;
string q;
If you are going to declare more than one variable of the same type then you can declare them in one statement like following:
int
x,y,z;
Declaring variables using C# data-types keywords
You notice that I colored the data types mentioned above in the blue color and you already know that C# keywords use the color blue and if you look at the table of C# keywords you will notice that each type of C# data types has a keyword in that table. Actually that's because we are using this keyword to tell the C# compiler that we need to declare a variable of a specific type. For example, to declare a variable of type int then you must use the keyword int in the statement to tell the C# compiler about this operation and if you need to declare a variable of type long then use the keywords long and the same thing with all built-in data types.
Declaring your variable inside a block of code
As we already know that a block of code is a series of related statements that delimited with "{ }". When you write C# code almost all the time you will write it inside a block of code so it"s critical for you to understand where you can use your declared variables.
When you declare a variable you can use it only in the block of code where you declared it or in a nested block of code only. To understand this we must give an example:
{
int x; // in this outer block you can use x only
{
int y; // in this inner block you can use both x and y
}
}