Pages

Wednesday, May 04, 2005

Boxing And Un-Boxing C# Reference

Boxing Conversion


Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. Boxing a value of a value allocates an object instance and copies the value into the new object.

Consider the following declaration of a value-type variable:

int i = 123;

The following statement implicitly applies the boxing operation on the variable i:

object o = i;

The result of this statement is creating an object o, on the stack, that references a value of the type int, on the heap. This value is a copy of the value-type value assigned to the variable i. The difference between the two variables, i and o, is illustrated in the following figure.

Boxing Conversion




It also possible, but never needed, to perform the boxing explicitly as in the following example:



int i = 123;
object o = (object) i;


Example



This example converts an integer variable i to an object o via boxing. Then the value stored in the variable i is changed from 123 to 456. The example shows that the object keeps the original copy of the contents, 123.



// boxing.cs
// Boxing an integer variable
using System;
class TestBoxing
{
public static void Main()
{
int i = 123;
object o = i; // Implicit boxing
i = 456; // Change the contents of i
Console.WriteLine("The value-type value = {0}", i);
Console.WriteLine("The object-type value = {0}", o);
}
}


Output



The value-type value = 456
The object-type value = 123


Unboxing Conversion








Unboxing is an explicit conversion from the type object to a value type or from an interface type to a value type that implements the interface. An unboxing operation consists of:


  • Checking the object instance to make sure it is a boxed value of the given value type.


  • Copying the value from the instance into the value-type variable.



The following statements demonstrate both boxing and unboxing operations:



int i = 123;          // A value type
object box = i; // Boxing
int j = (int)box; // Unboxing


The following figure demonstrates the result of the preceding statements.



Unboxing Conversion





For an unboxing conversion to a given value type to succeed at run time, the value of the source argument must be a reference to an object that was previously created by boxing a value of that value type. If the source argument is null or a reference to an incompatible object, an InvalidCastException is thrown.



Example



The following example demonstrates a case of invalid unboxing, of how incorrect unboxing leads to InvalidCastException. By using try and catch, an error message is displayed when the error occurs.



using System;
public class UnboxingTest
{
public static void Main()
{
int intI = 123;

// Boxing
object o = intI;

// Reference to incompatible object produces InvalidCastException
try
{
int intJ = (short) o;
Console.WriteLine("Unboxing OK.");
}

catch (InvalidCastException e)
{
Console.WriteLine("{0} Error: Incorrect unboxing.",e);
}
}
}


Output



System.InvalidCastException
at UnboxingTest.Main() Error: Incorrect unboxing.


If you change the statement:



int intJ = (short) o;


to:



int intJ = (int) o;


the conversion will be performed, and you will get the output Unboxing OK.