Before going through Boxing and Unboxing concept in C#, it’s important to know about value type and reference type variables. Please go through my article here.

Boxing :

It is the process of converting a value type variable to a reference type variable (object). That means the C#.Net converts a value type variable of type (int, float, bool etc) to an object which is a  reference type variable.

In below code snippet, we have declared a value type int x and then performing a boxing operation that converts the value type   x  to an object  obj  which is a reference type.

If we represent graphically, below image explains the memory allocation process for above code snippet. If you look closely,  we have declared a value type int x = 100 that is stored in the stack memory and the reference type obj variable is stored in the stack. (value of reference type stores in heap memory.)

Figure – 1 – Boxing

How the framework handle memory allocation during Boxing ?

During Boxing operation, when the compiler hits line obj = x  Here it determines that a Boxing operation has to be performed. Since we are trying to assign a value type to a reference type variable.

Boxing Operation
Figure – 2 – Boxing Operation

The compiler copies the integer value (100) from the stack, wraps up it as an object and allocates memory from the managed heap. It stores the memory address (memory address i.e 0x00000043) in the stack.

If you look at closely, now the reference type variable obj is pointing to the integer variable which is stored in the managed heap (memory address i.e  0x00000043).

So internally, during Boxing operation, the compiler allocates memory space from managed heap for the value type’s field. It then copies the value in to the new memory location. Value type is stored as a Reference type object in new memory location. Now the memory address is copied to the stack, which refers to the object.

Unboxing :

It is the process of converting a reference type (Object) variable in to a value type variable. That means the C#.Net unwraps the reference type variable (object) to a value type variable(int, float, bool etc ).

Extending the Boxing code snippet , In this above example, we are performing a Boxing operation and then, we have declared a value type variable int y  and assigning a reference type variable obj to it by doing the type cast.  The variable obj points to the boxed integer object int x in managed heap.

Figure – 3 – Unboxing

When the compiler hits the line int y = (int)obj .  It determines that it has to perform an Unboxing operation on the reference type (obj).  The compiler copies the  reference type’s  (obj) actual value (100) from object’s managed heap memory ( i.e – 0x00000043 ) and stores in the stack memory for the variable  y  which is a value type.

So internally, during the Unboxing operation, the compiler takes the boxed value type object which is on the managed heap memory. It  unwraps the value type’s value and copies it back to the stack memory.

Points to Remember: 

Boxing/Unboxing are helpful in some situations. However they might hurt application’s performance as they consume extra memory and steps to perform it.

During Unboxing operation, if the value type doesn’t match with the boxed object type  then an InvaidCastException will be thrown. As shown in the image below

Unboxing Invalid Cast Exception
Figure – 4 – Unboxing Invalid Cast Exception

In the above example, obj points to a boxed object of type int , However we are trying to Unbox it to type Long ,  So the compiler throws an InvalidCastException.