Boxing and Unboxing

All C# types, including the value types, are derived from object. Thus, a reference of type object can be used to refer to any other type, including value types. When an object reference refers to a value type, a process known as boxing occurs. Boxing causes the value of a value type to be stored in an object instance. Thus, a value type is “boxed” inside an object. This object can then be used like any other object. In all cases, boxing occurs
automatically. You simply assign a value to an object reference. C# handles the rest.
 
Unboxing is the process of retrieving a value from a boxed object. This action is performed using an explicit cast from the object reference to its corresponding value type. Attempting to unbox an object into a different type will result in a runtime error.
 
Here is a simple example that illustrates boxing and unboxing:

Example

using System;

namespace ConsoleApplication1
{
    class BoxingDemo
    {
        static void Main()
        {
            int x;
            object obj;
            x = 10;
            obj = x; // box x into an object
            int y = (int)obj; // unbox obj into an int
            Console.WriteLine(y);
        }
    }
}

This program displays the value 10. Notice that the value in x is boxed simply by assigning it to obj, which is an object reference. The integer value in obj is retrieved by casting obj to int.
 
Here is another, more interesting example of boxing. In this case, an int is passed as an argument to the Sqr( ) method, which uses an object parameter.

Example

using System;

namespace ConsoleApplication1
{
    // Boxing also occurs when passing values.
    class BoxingDemo
    {
        static void Main()
        {
            int x;
            x = 10;
            Console.WriteLine("Here is x: " + x);
            // x is automatically boxed when passed to Sqr().
            x = BoxingDemo.Sqr(x);
            Console.WriteLine("Here is x squared: " + x);
        }
        static int Sqr(object o)
        {
            return (int)o * (int)o;
        }
    }
}

Output

Here is x: 10
Here is x squared: 100

Here, the value of x is automatically boxed when it is passed to Sqr( ). Boxing and unboxing allow C#’s type system to be fully unified. All types derive from object. A reference to any type can be assigned to a variable of type object. Boxing and unboxing automatically handle the details for the value types. Furthermore, because all types are derived from object, they all have access to object’s methods. For example, consider the following rather surprising program:

Example

using System;

// Boxing makes it possible to call methods on a value!
namespace ConsoleApplication1
{
    class MethOnValue
    {
        static void Main()
        {
            Console.WriteLine(10.ToString());
        }
    }
}

This program displays 10. The reason is that the ToString( ) method returns a string representation of the object on which it is called. In this case, the string representation of 10 is 10!