C# Primitive Types and Variables
Data Types:
A data type communicates with the compiler informing it about the type of data that a particular variable can hold inside it. C# has several data types built inside it like Boolean, Integer, Float, Decimal, etc.
Whenever a variable is declared with a data type, the system allocates some memory space to store its values.
Data types are characterized by:
- Name – for example, int;
- Size (how much memory they use) – for example, 4 bytes;
- Default value – for example, 0.
Basic data types in C# are distributed into the following types and these data types are called primitive (built-in types), because they are embedded in C# language at the lowest level:
- Integer types – sbyte, byte, short, ushort, int, uint, long, ulong;
- Real floating-point types – float, double;
- Real type with decimal precision – decimal;
- Boolean type – bool;
- Character type – char;
- String – string;
- Object type – object.
Let’s have a look at some of the values and their ranges:
Primitive data types in C# have a direct correspondence with the types of the common type system (CTS) in the .NET Framework. For instance, int type in C# corresponds to System.Int32 type in CTS and to Integer type in VB.NET language, while long type in C# corresponds to System.Int64 type in CTS and to Long type in VB.NET language. Due to the common types system (CTS) in .NET Framework, there is compatibility between different programming languages (like for instance, C#, Managed C++, VB.NET, and F#). For the same reason int, Int32 and System.Int32 types in C# are actually different aliases for one and the same data type – signed 32-bit integer.
What Is a Variable?
In order to work with data we should use variables. We have already seen their usage in the examples, but now let’s look at them in more detail.
A variable is a container of information, which can change its value. It provides means for:
- storing information;
- retrieving the stored information;
- modifying the stored information.
Each of the variables in the C# environment has its own type, which is further defined by its characteristics like the type and size of value it can store. It also defines the set of operations that can be performed by the program to manipulate that value.
For example, we create a program that performs some calculations on the values entered by the user. The values entered by one user will obviously be different from those entered in by another user. This means that when creating the program, the programmer does not know what values will be introduced as input, and that makes it necessary to process all possible values a user may enter.
When a user enters a new value that will be used in the process of calculation, we can preserve it (temporarily) in the random-access memory of our computer. The values in this part of memory change (vary) throughout execution and this has led to their name – variables. A variable can be initialized and declared at the same time, or it can be initialized first and can be declared later.
In C# programming, you will use variables to store and process information all the time.
As I explained earlier that following are the basic value types in the C# that can be further categorized into data types:
- Integral types
- Floating
- Boolean
- Decimal
- Nullable
We can say that a variable is a named area of memory, which stores a value from a particular data type, and that area of memory is accessible in the program by its name. Variables can be stored directly in the operational memory of the program (in the stack) or in the dynamic memory in which larger objects are stored (such as character strings and arrays).
Classification Of Data Types (Value and Reference Types):
Data types can be broadly divided into two categories based on how the value is stored in their memory.
- Value Type
- Reference Type
Value Type: Primitive data types (numbers, char, bool) are called value types because they store their value directly in the program stack. Value types are stored in the program execution stack and directly contain their value. Value types are the primitive numeric types, the character type and the Boolean type: sbyte, byte, short, ushort, int, long, ulong, float, double, decimal, char, bool. The memory allocated for them is released when the program exits their range, i.e. when the block of code in which they are defined completes its execution.
For example, a variable declared in the method Main() of the program is stored in the stack until the program completes execution of this method, i.e. until it finishes (C# programs terminate after fully executing the Main() method).
Reference Type: Reference data types (such as strings, objects and arrays) are an address, pointing to the dynamic memory where their value is stored. They can be dynamically allocated and released i.e. their size is not fixed in advance contrary to the case of value types.
Reference types keep a reference (address), in the program execution stack, and that reference points to the dynamic memory (heap), where their value is stored. In simple words, a reference type variable just holds a reference to a particular memory location that may hold the required data.
Reference types allocate dynamic memory for their creation. They also release some dynamic memory for a memory cleaning (garbage collector), when it is no longer used by the program. If any changes are made to the data then the other variable will automatically inherit the new changed value and if there are no values assigned to the reference type then by default it contains a null value.
Reference types are all classes, arrays and interfaces such as the types: object, string, byte[].