What is Boxing and unboxing in C#
Boxing and Unboxing in C#
Boxing and unboxing are important concepts used in the C# type system. They are used to create a link between the two major data types in C#- Value type and Reference type. All value types are stored in the stack, but in some situations, they need to be referenced as the heap. Since reference types are stored in heap one can convert the value type into a reference type. This conversion is called boxing. Unboxing is the opposite operation of Boxing.
Boxing
Conversion of a value type to an object type is known as boxing. Boxing a value means allocating an object instance, and the value of the value type is copied into that object instance.
Example:
int Val=1;
object obj=val; //boxing
The first line creates a value type val and assigns 1 value to val. The second line creates an instance of object obj and assigns the value of val to obj. Thus, the boxing operation creates a copy of the value of the val integer to the object obj. When the compiler finds a value type in place of a reference type, it creates an object box into which it places the value of the value type. Now both the variables val and obj exist but the value of obj resides on the heap. This means that the value is independent of each other. Therefore, in the future, if there is a change in the value of obj the value of val is not affected.
For example, the value of obj is modified as given below; the value of val is not modified.
Int val= 256;
object obj=val;
obj=512;
Console.WriteLine(“val={0}, obj={1}”,Val, Obj);
See the following representation to understand the concept of boxing memory allocation:
From the figure, it is clear that the value of Obj is modified whereas the value of Val is not modified. The value type directly contains its data on stack whereas the Reference type is accessed indirectly using the reference variable obj that point to the data stored on the heap memory. The reference variable obj is stored on the stack. Thus, stack and heap are used by the reference variables in the boxing type conversion.
Advantages of Boxing
Boxing is useful in situations when a value type is converted into a Base object or to an interface. The CLR (Common Language Runtime) then converts the value type to reference type. CLR allocates memory on the heap and then copies the value type instance to it.
Disadvantages of Boxing
Following are the disadvantages of using boxing:
-
Boxing is an expensive process since it copies an object from a stack to heap which requires a number of processor cycles as well space on the heap.
-
The same object appears at two different places in memory which can have contradictory states.
Unboxing
The conversion of object type to value type is known as unboxing. Unboxing performs the opposite operation of boxing. Unboxing is done by an explicit conversion from the type object to any value type.
One can only unbox a variable that has already been boxed. Thus, it is very important to remember that unboxing requires an object type that is boxed from value type to object type, unboxing operation checks to ensure that the specified value type matches the type contained in the object type. If there is a mismatch, an error is reported. In other words, an unboxing operation consists of first checking that the object instance is a boxed value of the given value type, and then copying the value out the instance.
For example: object Obj=256;
int val=(int) Obj; //unboxing