-
C# Data Types
-
In previous chapter, we have discussed about variables, in this chapter, we will see, what are data types and how variables and datatypes are related.
Example:
There are 3 types of data types in C# language:int num = 7; // Integer double dbl = 7.77D; // Floating point number char letter = 'C'; // Character bool isBool = true; // Boolean string text = "Hello"; // String
- Value Types
- Reference Types
- Pointer Type
Therefore, It is important to use the correct data type for the corresponding variable; to avoid errors, to save time and memory, but it will also make your code more maintainable and readable.
Value Type
When we create a variable and assign a value to it directly. that is value type variable (Value type variables can be assigned a value directly). They are derived from the class System.ValueType. Value Type variables store memory on stack.Example:
The most common value data types are:int num = 7; // 7 is directly assigned to num variable which is of integer type.
Data Type Size int 4 bytes long 8 bytes float 4 bytes double 8 bytes bool 1 bit char 2 bytes string 2 bytes per character
As the name suggests, reference variables store references to the location where the actual data is stored - means, in case of reference variables, these variables does not store the actual value but they store the location of another variable where the actual value is assigned.
Unlike Value type Objects, Objects of reference types are stored on the managed heap in .NET, When we assign a reference type to a variable , we are actually copying the reference to the object, not the object itself. These Reference types are automatically managed by Garbage Collector(to retain the memory, once its job is done or if its not required).
Example: Arrays in C# are reference types, regardless of the data types they contain
Example of User defined Reference types are : Arrays, Class, Delegates, Interface, We will discuss these in later chapters.int[] numbers = new int[] { 1, 2, 3, 4, 5 };
Pointer Type
Pointer type variables used to store the memory address of another type.cIn C#, pointers have the same capabilities as the pointers in C or C++ but unlike C and C++ these are not used as frequently, because they are considered unsafe and can lead to security vulnerabilities and memory leaks if not used carefully.
Syntax for declaring a pointer type is −
It is important to note that while pointer types provide low-level memory manipulation capabilities but they are rarely needed in typical C# development. Most scenarios can be addressed using safe managed code constructs provided by the .NET framework.unsafe { int x = 100; int* ptr = &x; // Pointer to the memory address of variable 'x' Console.WriteLine("Value of x: " + *ptr); // Dereferencing the pointer to get the value *ptr = 200; // Modifying the value indirectly using the pointer Console.WriteLine("Modified value of x: " + x); }
Pointer types are primarily used in scenarios where performance optimizations or interop with unmanaged code are necessary.