All reference types and value types are derived from ‘System.Object’ class. So the reference variable of an object class can refer to any type.

Boxing and unboxing1
The process converting value type into object type or interface type which is implemented by value type is known as boxing and the reverse process of converting object type into value type is known as unboxing.

In below example integer variable is boxed;

int a=123; 
object obj=a;//Boxing 
int b=(int)obj;//Unboxing

 

In above code first value type variable a is boxed into object type and in the second step it is unboxed and assigned to value type variable b.

Let’s see boxing and unboxing in details;

Boxing: 

In boxing process, the value type is converted into the type object. In this process, CLR boxes the value type into System.Object. Normally value types are stored in stack memory but after boxing, they are a move to the managed heap. In boxing value types are treated as an object. Boxing is implicit and does not require any special syntax. In boxing, CLR creates object type instance on the heap and copies the value of value type into that new instance. As value is copied to new instance any changes made in the original value type variable does not reflect in object type instance.

using System; 
namespace BoxingDemo 
{ 
     class Program 
     { 
         static void Main(string[] args) 
         { 
             int num = 123; 
             object obj = num;   //Boxing num into obj instance 
             Console.WriteLine("The value of variable num is {0}",num); 
             Console.WriteLine("The value of instance obj is {0}", obj); 
             num = 456; 
            //Values of num and obj after changing value of num 
             Console.WriteLine("The value of variable num is {0}", num); 
             Console.WriteLine("The value of instance obj is {0}", obj); 
             Console.ReadLine(); 
         } 
     } 
}

 

Recommended Post   C# Type Conversion

The output of above program is: 

The value of variable num is 123 
The value of instance obj is 123 
The value of variable num is 456 
The value of instance obj is 123

 

Unboxing:

Unboxing is the reverse process of boxing. In this process, the object type is converted to value type.  This is explicit conversion. For boxing, the cast operator is used. In unboxing CLR copies the value of object type instance and stores it in stack memory.

For unboxing the object instance must be boxed previously from value type. Trying to unbox reference to an incompatible value type generates ‘InvalidCastException’.If someone tries to unbox null reference then it will cause a ‘NullReferenceException’.

For Example,

using System; 
namespace UnBoxingDemo 
{ 
    class Program 
    { 
         static void Main(string[] args) 
        { 
            int num = 100; 
            object obj = num;       //Boxing num variable into obj 
            int num1 = (int)obj;    //Unboxing value from obj and assigned to num1 
            //int num1 = (short)obj;  //This will generate InvalidCastException 
            Console.WriteLine("The value of num is {0}",num); 
            Console.WriteLine("The value of num1 is {0}", num1); 
            Console.ReadLine(); 
        } 
    }
}

 

The output of above program is: 

The value of num is 100 
The value of num1 is 100

 

Note: 

It is good programming practice to avoid boxing and unboxing. Boxing and unboxing processes slow down the performance. In these processes, the variable is jumped from stack to heap and vice versa which is an expensive process. So it is better to avoid boxing and unboxing whenever it is possible.

SHARE
Previous articleC# OOPS
Next articleC# Data Types & Variables