Tag Archives: Value type

Stack, heap, Value types, Reference types, boxing, and unboxing

When you declare a variable in .Net, it allocates some chunk of memory in RAM. This memory has three things :
name
data type &
value of the variable.
Depending upon the data type there are two types of memory allocation: Stack and Heap


The Stack is responsible for keeping track of the running memory needed in your application.

Heap doesn’t track the running memory. It is just a pile of object which can be reached at any time. It is dynamic memory allocation.

eg:

public void Method1()
{
// Line 1
int i=4;

// Line 2
int y=2;

//Line 3
class1 cls1 = new class1();
}

Line1: When Line1 is executed the compiler allocate small chunk of memory in the Stack.

Line2: It allocates memory for y on top of the first memory in Stack (LIFO).
Line3: Here an object is created. When this line is executed it creates a pointer on Stack and the actual object is stored in a Heap memory.

NB: Reference pointer is allocated on Stack. The statement
Class1 cls1;
doesn’t allocate memory for an instance of Class1, it only allocates a stack variable cls1 (and sets it to null). The time it hits new keyword, it allocates on “heap”.

Exiting the above Method
On exiting the method, it clears all the memory variable which are assigned on Stack.
But it didn’t de-allocate the heap memory. This memory will be later de-allocated by the garbage collector.

Now we have discussed about the Stack & Heap memory. Let’s move on to Value & Reference Types.

Value types
1. Value types are types which hold both memory & data on the same location i.e Stack.
2. Below we assign an int value (i) to another int value (y). Both these memory are separately allocated on stack. So if we change either of them then other doesn’t change. This is Value type.


Reference Type
1. A reference type has a pointer which points to the memory location.
2. Now when we create an object and assign an object to another object, they both point to same memory location (in heap). So if we change one of them, the other object is also affected. This is Reference type.


Type Chart


Boxing and Unboxing

Boxing
When we move data from Value type to Reference type, data is moved from Stack to Heap. It is termed as Boxing.

Unboxing
When we move data from Reference type to Value type, data is moved from Heap to stack. It is termed as Unboxing.

References:

http://www.codeproject.com/Articles/76153/Six-important-NET-concepts-Stack-heap-value-types

6 Comments

Filed under C#

Nullable Type

Nullable types are instances of the System.Nullable struct.

Variables are of two types.

Value type eg. int, char, date. A data type is a value type if it holds the data within its own memory allocation.
Reference type eg. string, object. A reference type contains a pointer to another memory location that holds the data.
 For more details on Value types and Reference type of variable, please read http://msdn.microsoft.com/en-us/library/t63sy5hs(VS.80).aspx

Reference types variable can be assigned null but value types of variables like integer can’t because they can’t contain reference, even the reference of nothing.

int i = 0;
The above statement shall throw error because we are trying to store null in the integer variable.

To avoid this we can use nullable modifier like this.

int? ii = null;
In the above code, ii is the nullable type of variable that can store either null or any integer type of variable. When a variable is declared as nullable, its extends two properties called HasValue and Value. Both property are public and read-only.

Leave a comment

Filed under C#