C# Value Types

Value Types and Reference Types are two main data types in C#. This separation for value type and  reference type are done based on the way how each type stores its value in memory. I will be covering Reference type in my next blog. We will discuss in detail about Value type here.

What is Value Types ?
  • Value types are stored generally(not always) in the stack. A data type is a value type if it holds a data value within its own memory space have their own copy of the data
  • With value types, each variable has its own copy of the data, and it is not possible for operations on one variable to affect the other (except in the case of in, ref and out parameter variables).
Type of value types in C#
  • Built-in value types
    • int, bool, byte, char, decimal, double, float, long, sbyte, short, uint, ulong, ushort
  • User-defined value types
    • struct
    • enum
Storage locations
  • The CLR stores objects in three types of storage locations -- the CPU registers, the stack or the managed heap. The short-lived objects are stored inside registers or stack, and the long-lived objects are stored in the heap. As defined, value types are generally stored in the stack. It's a general misconception that value types are always stored in the stack.  
  • Value types are stored on the stack when the value is a local variable or lives inside the local memory pool. However, value type are stored on the heap when they are a part of other objects on the heap or are a static field. And they always can be stored inside CPU register as a part of evaluation stack processing. The actual location of a value type depends on the implementation of the JIT compiler.
  • Most of the primitive types, such as intfloatdouble, and char have a fixed size, and when you declare a variable as a value type, the compiler generates code that allocates a block of memory big enough to hold a corresponding value. For example, declaring an int variable causes the compiler to allocate 4 bytes of memory (32 bits) to hold the integer value. A statement that assigns a value (such as 42) to the int causes the value to be copied into this block of memory.
Note : Value type can be stored in a stack frame, in the CPU register or even in the heap memory if the value type is contained inside an object, i.e., if it is a part of a reference type.
                                

Examples :

1. Value assignment

int x = 50; // declare and initialize x 
int y = x; /* y will have a separate storage in memory with same data as variable x. 
x++; /* Even if we increment the variable x, it will not impact y; 
Output : x now has a value of 51, but y still contains 50
Memory allocation is explained below for the above program

2. Passing the Value Type Variables

The system creates a separate copy of a variable in another method when you pass a value-type variable from one method to another. If value got changed in the one method, it wouldn't affect the variable in another method.

Code :

static void ChangeValue(int y){
   y = 200;
   Console.WriteLine($"second output {y} ");
}
static void Main(string[] args){
    int x = 50;
    Console.WriteLine($"first output {x} ");
    ChangeValue(x);
    Console.WriteLine($"Third output {x} ");
}

Output 

first output 50
second output 200
Third output  50

Post a Comment

Previous Post Next Post