Boxing and Unboxing in C#

This article talks about one of the most interesting and a little bit puzzling concept of C#.

Boxing

Any type value or reference can be assigned to an object without an explicit conversion. When the compiler finds a value type where it needs a reference type, it creates an object box into which it places the value.

Explanation

First of all creating a box of int type (10 bytes) and then creating an object "om" and allocating memory to it.

  1. int m = 10;  
  2. object om = m;  
  3. // creates a box and hold m in it 

We can also do that through casting.

  1. int m = 10;  
  2. object om = (object)m;  
  3. //C style casting  

Reference Example

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ConsoleApplication2  
  7. {  
  8.     class box  
  9.     {  
  10.         public static int k  
  11.         {  
  12.   
  13. (ing properties to the method.)  
  14.   
  15.             get;  
  16.             set;  
  17.         }  
  18.         public static void Main()  
  19.         {  
  20.   
  21.             // -- Statement for unboxing –  
  22.   
  23.             int i = 5;  
  24.             object j = i;  
  25.   
  26.             // ----------------------------------------  
  27.   
  28.             // -- Statement for unboxing --  
  29.             //int i = 5;  
  30.             //object j = i;  
  31.             //i = (int)j;  
  32.   
  33.             // ----------------------------------------  
  34.   
  35.             Console.WriteLine("Enter val of k: ", k);  
  36.             k = Convert.ToInt32(Console.ReadLine());  
  37.   
  38.             if (i <= k)  
  39.             {  
  40.                 i += k;  
  41.                 Console.WriteLine("\n Sum is: {0}", i);  
  42.             }  
  43.             else  
  44.             {  
  45.                 Console.WriteLine("\n Your stucked!");  
  46.             }  
  47.             Console.ReadLine();  
  48.         }   
  49.     }   

Output

Screen: 1

Creating a box or a container (Boxing):

Boxing

Value

Screen: 2

Result

Quick Points

  • It converts a value type into a reference type.
  • Values are stored in the Stack first then moved to the heap.
  • Creates a container/box for holding the value.

Unboxing

Unboxing is simply the opposite of boxing. In it values are again shifted from the heap to the stack. In unboxing first of all it checks for the boxed object value and then it goes for replica creation or we can say for copying.

Explanation

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ConsoleApplication2  
  7. {  
  8.     class box  
  9.     {  
  10.         public static int k  
  11.         {  
  12.   
  13. (ing properties to the metho)  
  14.   
  15.             get;  
  16.             set;  
  17.         }  
  18.         public static void Main()  
  19.         {  
  20.   
  21.             // -- Statement for unboxing –  
  22.   
  23.             //int i = 5;  
  24.             //object j = i;  
  25.   
  26.             // ----------------------------------------  
  27.   
  28.             // -- Statement for unboxing --  
  29.             int i = 5;  
  30.             object j = i;  
  31.             i = (int)j;  
  32.   
  33.             // ----------------------------------------  
  34.   
  35.             Console.WriteLine("Enter val of k: ", k);  
  36.             k = Convert.ToInt32(Console.ReadLine());  
  37.   
  38.             if (i <= k)  
  39.             {  
  40.                 i += k;  
  41.                 Console.WriteLine("\n Sum is: {0}", i);  
  42.             }  
  43.             else  
  44.             {  
  45.                 Console.WriteLine("\n Your stucked!");  
  46.             }  
  47.             Console.ReadLine();  
  48.         }   
  49.     }   

Quick Points

  • It is the opposite process of boxing.
  • It converts an object type back into the value type.
  • It is an explicit operation using C-style casting.

Up Next
    Ebook Download
    View all
    Learn
    View all