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:

int num = 7;               // Integer 
double dbl = 7.77D;  // Floating point number
char letter = 'C';         // Character
bool isBool = true;          // Boolean
string text = "Hello";     // String

There are 3 types of data types in C# language:

  1. Value Types
  2. Reference Types
  3. Pointer Type
Data types in C# represent the type of data that a variable can hold.

1. They define the size and layout of the variable's memory and the operations that can be performed on the variable.
2. Data types are essential because they allow the compiler to allocate memory and perform operations efficiently based on the type of data being stored.
3. They also provide type safety, preventing unintended data conversions and errors during program execution.

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:

int num = 7;      // 7 is directly assigned to num variable which is of integer type.

The most common value data types are:

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

Reference Type
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

int[] numbers = new int[] { 1, 2, 3, 4, 5 };

Example of User defined Reference types are : Arrays, Class, Delegates, Interface, We will discuss these in later chapters.

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 −

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);
}

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.
Pointer types are primarily used in scenarios where performance optimizations or interop with unmanaged code are necessary.