Tuesday, February 14, 2012

Value Types Vs Reference Types in .NET


Before we dive into the value types or reference types definition, let's take a step back and understand what happens when you declare a variable .NET.

Anytime you declare a variable, some memory is allocated to this variable. There are three pieces of information that is stored in memory - the variable name, its datatype and the value it contains. The variable is stored on one of the two types of memory - Stack and Heap.

When you declare a variable which is of value type (I know I am jumping ahead), some memory is allocated. When you declare another variable of the value type, another chunk of memory is allocated which is stacked on top of the first allocation and so forth. Think of stacks as just boxes stacked on top of one another. For value types the memory is allocated statically. For example, 4 bytes of memory is allocated to a variable of type Int.

Memory is allocated on a first come first serve basis but it is de-allocated in the reverse order i.e. Last In First Out (LIFO). Think of it as you are stacking boxes on top of each other and to remove them, you have to remove the last one first and so on.

When you create a variable of type reference, the object itself is allocated on another type of memory called "Heap". Just as the name suggests, heap is just a collection of reference type variables (also commonly called objects) and there is no particular order. A pointer to these reference type variables is stored on stack.

Memory De-Allocation
When you exit a method or a class where you instantiated and consumed these variables, memory is de-allocated in the following fashion

  • Stack is cleared first in LIFO order.
  • Heap is not cleared - Garbage Collector will clear it at some point
You may be wondering why value types are stored on stack and reference types are stored on heap?

The reason primarily has to do with the complexity. Value types are primitive data types such as Integer, Byte, SByte etc. and framework knows in advance how much memory to allocate when they are initialized. Reference types such as objects on the other hand are complex types. An object may have an instance of another object etc. which means the memory must be dynamically allocated, hence they are stored on a heap.

Another point to note is that all variables must be declared and instantiated, but .NET framework automatically instantiates the value types.

For example - 
int i = new int(5);  
can also be declared as int i = 5;

All reference types with the exception of String must be explicitly instantiated (unless your classes are declared as static/shared).

For example
Person clsPerson = new Person();

Value Types
  • All Numeric Data Types 
    • E.g. integer, byte, sbyte, short, Decimal, Double etc.
  • Variables of type Boolean, Char and Date
  • Enumerations
    • Since the underlying types for enumerators can only be of value type.
  • Structs / Structures
Reference Types
  • String
  • Arrays (even if their elements are value types)
  • Classes / Forms.
  • Delegates

In the future post we will discuss the concept of boxing and unboxing and the difference between structures and classes and performance impacts of using one over the other.

Thank you and as always, your comments are welcome!




No comments:

Post a Comment