Thursday, July 23, 2009

Boxing & UnBoxing

C# provides two types of variables they are Value types and Reference Types. Value Types are stored on the stack and Reference types are stored on the heap. The conversion of value type to reference type is known as boxing and converting reference type back to the value type is known as unboxing.

Example:

class Test
{
    static void Main() {
        int i = 1;
        object o = i;        // boxing
        int j = (int) o;     // unboxing
    }
}


No comments: