Tuesday, December 25, 2018

Boxing & Unboxing

Boxing & Unboxing:

                  Consider a situation where we are passing value to a method but we don’t know what type of value we are going to pass. In this kind of situation we have to use “object” data type. The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). The object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. Object is reference type which can store any kind of value including value type and reference type. Before assigning values, it needs type conversion. Object type can be used to hold the data. Before using it in the expression it needs conversion to its original type.  

For Example:
class Program
    {
        static void Main(string[] args)
        {
            int a = 34;
            double b = 345.56;
            String str = "Hello";
            Boolean flag = true;
            func(a);
            func(b);
            func(str);
            func(flag);
        }

        static void func(object ob)
        {
            Console.WriteLine(ob);       
        }
    }
            In above code the function func has parameter of object type that means we can pass any kind of parameter to the function.  When a variable of a value type is converted to object, it is said to be boxed. When a variable of type object is converted to a value type, it is said to be unboxed. The Object does not allow you to perform any operation such as mathematical operation without doing "boxing and unboxing".

Boxing:

Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type. In boxing any type value or reference can be assigned to an object without an explicit conversion. When the CLR boxes a value type, it wraps the value inside a System.Object. In boxing process, a value type is being allocated on the heap rather than the stack.  

For Example:

int stackVar = 12;
object boxedVar = stackVar;                       // Boxing

Unboxing:

Unboxing is simply the opposite of boxing. When value is boxed into an object type, it cannot be used in operations. An unboxing conversion permits an explicit conversion from type object to any value-type. It is explicit conversion of same reference type (which is being created by boxing process); back to a value type. In unboxing process, boxed value type is unboxed from the heap and assigned to a value type which is being allocated on the stack.

For Example:

int stackVar = 12;
object boxedVar = stackVar;                       // Boxing
stackVar = (int)boxedVar;                          // Unboxing 

Sometimes boxing is necessary, but you should avoided it if possible, since it will slow down the performance and increase memory requirements. For example, when a value type is boxed, a new reference type is created and the value is copied from the value type to the newly created reference type. This process takes time and required extra memory (around twice the memory of the original value type). Attempting to unbox a null causes a NullReferenceException.

Difference between Boxing & Unboxing:




Prof. Shardul P. Patil

profshardulp.patil@gmail.com

No comments:

Post a Comment