Boxing and Unboxing

Boxing and Unboxing in C# are conversion from value types to reference types and vice versa.

Description
 
Before we talk about boxing and unboxing in C#, let's understand C# types. C# supports two types of types - value type and reference type. 
 
Value Types
 
A variable representing an object of value type contains the object itself. (It does not contain a pointer to the object). Different value types are: 
 
  • Simple Types: Integral Types (sbyte, byte, short, ushort, int, uint, long, ulong), bool type, char type, Floating point types(flaot,double) and the decimal types. They are all aliases of the .NET System Types.
  • Struct Types 
  • Enumeration Types
  1. System.Int32 a= 10; //It is a value type
The value type objects can not be allocated on the managed heap.
 
Reference Types
 
A variable representing an object of reference type contains reference or address of the actual data. Reference types are allocated on the managed heap. Different reference types are: 
  • The Object Type
  • The class Type
  • Interfaces
  • Delegates
  • The string type
  • Arrays
  1. Myobj obj1; // obj1 is ia reference type assuming Myobj is of class type.  
  2. obj1=new myobj();
obj1 is reference type variable (assuming Myobj is a class type variable).compiler allocates memory for this object on the heap and its address is stored in obj1.
 
Boxing And Unboxing
 
Having talked about these value types and reference types, now let us talk about boxing and unboxing. Converting a value type to a reference type is called Boxing. Unboxing is an explicit operation. You have to tell which value type you want to extract from the object.
 
Consider the following code:
  1. Int32 vt= 10; //value type variable  
  2. object rt= vt; /*memory is allocated on the heap of size equal to size of vt,the value type bits are copied to the newly allocated memory and the address of the object is returned and stored in rt.This is basically called Boxing.*/  
  3. Int32 vt2=(Int32)rt;//Unboxing  
Source Code 
  1. using System;  
  2. class BoxAndUnBox  
  3. {  
  4. public static void Main()  
  5. {  
  6. Int32 vt= 10; //value type variable  
  7. object rt= vt; /*memory is allocated on the heap of size equal to size of vt,the value type bits are copied to the newly allocated memory and the address of the object is returned and stored in rt.This is basically called Boxing.*/  
  8. vt=50;  
  9. Int32 vt2=(Int32)rt;//Unboxing  
  10. Console.WriteLine(vt+","+ rt);  
  11. }  
  12. }  
Here is another article on Boxing and Unboxing in C#