Tech News

Difference between a Value Type and a Reference Type?

The Types in .NET Framework are either treated by Value Type or by Reference Type. A Value Type holds the data within its own memory allocation and a Reference Type contains a pointer to another memory location that holds the real data. Reference Type variables are stored in the heap while Value Type variables are stored in the stack.

In a simple sentance the Value Type holds the data within its own memory allocation and a Reference Type contains a pointer to another memory location that holds the real data.
Value Type:
A Value Type stores its contents in memory allocated on the stack. When you created a Value Type, a single space in memory is allocated to store the value and that variable directly holds a value. If you assign it to another variable, the value is copied directly and both variables work independently. Predefined datatypes, structures, enums are also value types, and work in the same way. Value types can be created at compile time and Stored in stack memory, because of this, Garbage collector can’t access the stack.
e.g. int x = 10;
Here the value 10 is stored in an area of memory called the stack.

Reference Type:
Reference Types are used by a reference which holds a reference (address) to the object but not the object itself. Because reference types represent the address of the variable rather than the data itself, assigning a reference variable to another doesn’t copy the data. Instead it creates a second copy of the reference, which refers to the same location of the heap as the original value. Reference Type variables are stored in a different area of memory called the heap. This means that when a reference type variable is no longer used, it can be marked for garbage collection. Examples of reference types are Classes, Objects, Arrays, Indexers, Interfaces etc.
e.g. int[] iArray = new int[20];
In the above code the space required for the 20 integers that make up the array is allocated on the heap.
……………………………………………………………………………………………………………………………………………….
Example:
Reference is usually associated with a pointer. Meaning the memory address where your variable reside is actually holding another memory address of the actual object in a different memory location.
Thus, when you do something like:
var a = “Hello”;
the computer will do the following:
  1. allocate memory (say starting at memory location 1000 for 5 bytes) and put H (at 1000), e (at 1001), l (at 1002), l (at 1003) and o (at 1004).
  2. allocate somewhere in memory (say at location 0500) and assigned it as the variable a.
    So it’s kind of like an alias (0500 is a).
  3. assign the value at that memory location (0500) to 1000 (which is where the string Hello start in memory). Thus the variable a is holding a reference to the actual starting memory location of the “Hello” string.
Value type will hold the actual thing in its memory location.
Thus, when you do something like:
var a = 1;
the computer will do the following:
  1. allocate a memory location say at 0500 and assign it to variable a (the same alias thing)
  2. put the value 1 in it (at memory location 0500).
    Notice that we are not allocating extra memory to hold the actual value (1). Thus a is actually holding the actual value and that’s why it’s called value type.
………………………………………………………………………………………………………………………………………………
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x