Monthly Archives: October 2012

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 :
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.


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

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

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



Filed under C#

Aggregation & Composition

Aggregation means “has-a” relationship. Here the two entity are dependent on each other but the can exist alone as well.
eg: Person and Shirt entity. Shirt is dependent upon the person who wears it, but it can exist alone as well.


Composition means “is-a” relationship. Here the two entity are tightly dependent on each other and can’t exist alone.
Eg: Human and Heart they both are dependent on each other and can’t exist alone.

Leave a comment

Filed under C#

Abstraction vs Encapsulation

Below is the difference:



Encapsulation is the act of packing data and function into a single unit.

Abstraction means representing the essential features without representing/hiding the background details.

Eg: Class

Eg: abstract class, interface.

Encapsulation is achieved using abstraction.

It is achieved using access modifier.





Here if we make Validate() and CreateBDObject() methods public and let the client call these method before calling Add() then it will be showing extra information to user as these two methods will only be called if we want to perform Add operation.


So if we follow abstraction then we make these two methods private and include them in Add() method. This way only the essential features are shown to the client and hiding the background details.

Leave a comment

Filed under C#

How to Dispose a Static member?

Static finalizer & dsetructor are not possible and these types are unloaded only when the AppDomain shuts down.

However, to make the Static object ready for Garbage collection, you can assign it to null.

static List<Customers> customers = new List<Customers>();

customers = CustomerDAC.GetCustomers();

//To unload static class

customers = null;

Leave a comment

Filed under C#

Static vs Singleton

Though both restrict to have only one instance of the class to be created but there are differences:



It doesn’t maintain any state as no object is created. It maintains the state of object.
Static class gets activated at run-time. Singleton follows lazy loading, so object gets created when required.
It doesn’t implement interface. It implement interface.
Disposing Static is not possible, as types are only unloaded when the AppDomain shut down. They can be disposed at run time.
We can’t create object of Static class. Singleton object can be used as parameter.


Leave a comment

Filed under C#

Static members in Generic class

Yes it is possible as shown below but:
Bear in mind that for every Generic type you get a different Static variable. SO if the Static member value has nothing to do with the Generic type then they shouldn’t be in Generic class. They should be declared in different class. 

Leave a comment

Filed under C#