Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • .Net Boxing and Unboxing explained with example

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 94
    Comment on it

    Boxing

    The automatic conversion of value type e.g int,char,float etc to a reference type e.g object is termed as Boxing. In general the value type variables are stored in memory space called stack but whenever there is a boxing process the value type variables are stored in heap rather than stack. 

    UnBoxing

    The reverse process of Boxing is Unboxing which is explicit conversion of reference type to a value type.

    Example of Boxing and UnBoxing Process in .Net C#:

    using System;
    
    namespace ConsoleApplication2
    {
        public class BoxingExample
        {
            static void Main()
            {
                int a = 461,b;  //A value type variable a is declared and initialized.
    
                object ob = a;  // Boxing,implicit conversion of value type a to reference type ob i.e copies the value of a into object ob.
    
                a = 100;   // The value of value type variable a is changed.
    
                b = (int)ob;  //Unboxing
    
                // The value stored in ob does not changes as value of a changes.
                System.Console.WriteLine("The value-type value of a = {0}", a);
                System.Console.WriteLine("The reference-type value of ob after boxing process in a = {0}", ob);
                System.Console.WriteLine("The value-type value of b after unboxing process in ob = {0}", ob);
                Console.ReadKey();
            }
    
        }

    Output:

    The value-type value of a = 100
    The reference-type value of ob after boxing process in a = 461
    The value-type value of b after unboxing process in ob = 461


    Explanation:

    In the above example a value type "a" is defined and initialized. The memory is allocated to this variable on the stack. The boxing process takes place in the statement:

    object ob = a;

    This statement causes a reference type(object) "ob" to be created on the stack which is pointing to a value which was of value type on the heap i.e "ob" contains the address of value "a" on the heap. Changing value of value type "a" only changes value to which memory was allocated on stack not of value to which "ob" is pointing. 

    The UnBoxing process takes place in the statement:

    b = (int)ob;

    "b" is a value type variable to which memory is allocated on stack and value assigned to it is unboxed value of object type "ob" i.e "b" is an int type which is to be assigned to an int value thus "ob" value is explicitly converted to an int value and assigned to "b" thus unboxing process takes place.

    Few points to consider:

    1.Boxing should be avoided because memory requirements are increased with this process thus slowing down the performance.

    2.An InvalidCastException occurs whenever an attempt is being made to unbox a reference type to an incompatible value type. In above example if value "b" was a float type variable then statement b = (int)ob will give an InvalidCastException.

    3.A NullReferenceException occur when whenever an attempt is being made to unbox a null value.E.g:

    int a = null;
    object ob = a;
    int b = (int)ob; // NullReferenceException

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: