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.

int m = 10;  
object om = m;  
// creates a box and hold m in it 

We can also do that through casting.​​​​​​​

int m = 10;  
object om = (object)m;  
//C style casting  

Reference Example

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace ConsoleApplication2  
{  
    class box  
    {  
        public static int k  
        {
           (ing properties to the method.)
            get;  
            set;  
        }  
        public static void Main()  
        {  
  
            // -- Statement for unboxing –  
  
            int i = 5;  
            object j = i;  
  
            // ----------------------------------------  
  
            // -- Statement for unboxing --  
            //int i = 5;  
            //object j = i;  
            //i = (int)j;  
  
            // ----------------------------------------  
  
            Console.WriteLine("Enter val of k: ", k);  
            k = Convert.ToInt32(Console.ReadLine());  
  
            if (i <= k)  
            {  
                i += k;  
                Console.WriteLine("\n Sum is: {0}", i);  
            }  
            else  
            {  
                Console.WriteLine("\n Your stucked!");  
            }  
            Console.ReadLine();  
        }   
    }
}

Output

Screen: 1

Creating a box or a container (Boxing):

Boxing and Unboxing in C#

Boxing and Unboxing in C#

Screen: 2

Boxing and Unboxing in C#

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

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace ConsoleApplication2  
{  
    class box  
    {  
        public static int k  
        { 
           (ing properties to the metho)
            get;  
            set;  
        }  
        public static void Main()  
        {  
  
            // -- Statement for unboxing –  
  
            //int i = 5;  
            //object j = i;  
  
            // ----------------------------------------  
  
            // -- Statement for unboxing --  
            int i = 5;  
            object j = i;  
            i = (int)j;  
  
            // ----------------------------------------  
  
            Console.WriteLine("Enter val of k: ", k);  
            k = Convert.ToInt32(Console.ReadLine());  
  
            if (i <= k)  
            {  
                i += k;  
                Console.WriteLine("\n Sum is: {0}", i);  
            }  
            else  
            {  
                Console.WriteLine("\n Your stucked!");  
            }  
            Console.ReadLine();  
        }   
    }   
}

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.


Similar Articles