Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding Boxing and Unboxing in C#

 Print 
  Author : Shailendra Chauhan
Posted On : 12 Oct 2013
Total Views : 153,726   
Updated On : 26 Sep 2016
 

Boxing and unboxing are the most important concepts you always get asked in your interviews. Actually, it's really easy to understand, and simply refers to the allocation of a value type (e.g. int, char, etc.) on the heap rather than the stack.

Boxing

Implicit conversion of a value type (int, char etc.) to a reference type (object), is known as Boxing. In boxing process, a value type is being allocated on the heap rather than the stack.

Unboxing

Explicit conversion of same reference type (which is being created by boxing process); back to a value type is known as unboxing. In unboxing process, boxed value type is unboxed from the heap and assigned to a value type which is being allocated on the stack.

Boxing and Unboxing

For Example

// int (value type) is created on the Stack
int stackVar = 12; 

// Boxing = int is created on the Heap (reference type)
object boxedVar = stackVar; 

// Unboxing = boxed int is unboxed from the heap and assigned to an int stack variable
int unBoxed = (int)boxedVar;

Real Life Example

int i = 10;
ArrayList arrlst = new ArrayList();

//ArrayList contains object type value
//So, int i is being created on heap
arrlst.Add(i); // Boxing occurs automatically

int j = (int)arrlst[0]; // Unboxing occurs

Note

  1. Sometimes boxing is necessary, but you should avoided it if possible, since it will slow down the performance and increase memory requirements.

    For example, when a value type is boxed, a new reference type is created and the value is copied from the value type to the newly created reference type. This process takes time and required extra memory (around twice the memory of the original value type).

  2. Attempting to unbox a null causes a NullReferenceException.

    int? stackVar = null;
    // Boxing= Integer is created on the Heap
    object boxedVar = stackVar;
    
    // NullReferenceException
    int unBoxed = (int)boxedVar; //Object reference not set to an instance of an object.
    
  3. Attempting to unbox a reference to an incompatible value type causes an InvalidCastException.

    int stackVar = 12;
    // Boxing= Integer is created on the Heap
    object boxedVar = stackVar;
    
    // InvalidCastException
    float unBoxed = (float)boxedVar; //Specified cast is not valid.
    
What do you think?

I hope you will enjoy the tips while programming with C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS
24 SEP
Angular2 and Angular4 (Classroom)
08:30 AM-11:30 AM IST
23 SEP
MEAN Stack (Classroom)
8:30 Am To 11:30 Am
23 SEP
Microsoft Azure Infrastructure Solutions (Online)
08:00 PM-11:00 PM IST / 9:30 AM-12:30 PM CST
20 SEP
MEAN Stack (Online)
07:00 AM-09:00 AM IST
20 SEP
ASP.NET MVC with Angular4 (Online)
9:00PM- 11:00PM IST(+5:30GMT)
16 SEP
Angular2 and Angular4 (Online)
08:00 AM-10:00 AM IST(+5.30 GMT)
7 SEP
ASP.NET MVC with Angular4 (Online)
09:00 PM to 11:00 PM IST (+5:30 GMT)
22 AUG
ASP.NET Core with Angular4 (Online)
07:00 AM - 9:00 AM IST(+5:30 GMT)
19 AUG
MEAN Stack 2 (Online)
5:30 Pm - 7:00 PM IST (+5:30 GMT)
19 AUG
ASP.NET MVC with Angular4 (Online)
10:30 Am to 12:30 PM IST (+ 5:30 GMT)
19 AUG
NodeJS with Angular4 (Classroom)
04:00 PM to 07:00 PM IST (+5:30 GMT)
12 AUG
ASP.NET MVC with Angular4 (Classroom)
11:30 AM - 1:30 PM IST (+5:30GMT)
15 JUL
ASP.NET MVC with Angular4 (Online)
03:30 PM - 05:30 PM IST(+5.30 GMT)
LIKE US ON FACEBOOK
 
+