In C#, all the value types are derived from the class object. So, a reference variable of type object can refer to any other value type. C# introduces two methods to Boxing and Unboxing, which links value type to reference type.
The basic difference between Boxing and Unboxing is that Boxing is the conversion of the value type to an object type whereas, on other hands, the term Unboxing refers to the conversion of the object type to the value type. Let us study the other differences between Boxing and Unboxing.
Content: Boxing Vs Unboxing in C#
|Basis for Comparison||Boxing||Unboxing|
|Basic||Object type refers to the value type.||process of retrieving value from the boxed object.|
|Storage||The value stored on the stack is copied to the object stored on heap memory.||The object's value stored on the heap memory is copied to the value type stored on stack.|
|Conversion||Implicit conversion.||Explicit conversion.|
|Example||int n = 24;|
object ob = n;
|int m = (int) ob;|
Definition of Boxing
Boxing is a procedure of converting a value type to an object type. Here, the value type is stored on the stack, and the object type is stored in the heap memory. This conversion of value type to the object type is an implicit conversion. You can directly assign a value to an object, and C# will handle the rest conversion. Let’s understand Boxing with an example.
int i = 24; object ob = i; // Box the integer type n into object type ob. or object ob1=21; // here also an object type ob1 refers to an integer type
In above code, the integer type i containing value 24 is stored on the stack and is copied to the object type ob. An object type is now referring to an integer value. Now, the “int i” also contain value 24 and the “object type ob” also contain value 24, but both the values are independent of each other i.e. if you change the value of i, it won’t reflect the change in the value of ob.
Boxing consumes extra memory along with extra time. The reason is that a new object, which will refer to the value type, must allocate the memory space on the heap. Next, the value of the value type that is stored on the stack will be transferred to ob the object type, on the heap memory location.
Definition of Unboxing
The reverse of Boxing is Unboxing. Unboxing is a conversion of the object type to the value type. In Unboxing the value of boxed object type stored on the heap is transferred to the value type that is stored on the stack. Unlike Boxing, the Unboxing has to be done explicitly. The object type is explicitly cast to the value type, and the value type must be same as value the object type is referring to. Let’s understand the concept of Unboxing with an example.
int i = 24; object ob = i; // Box the integer type n into object type ob. int j = (int) ob; // Unbox the integer value stored in object type ob to integer type y.
The value stored in the object ob is retrieved by casting it to the type same as the object was referring to i.e. integer type “j”.
Unboxing also consumes more memory and more time. Since, when an object type has to be unboxed then the value of the object type stored on the heap has to be transferred to the new value type stored on the stack. The object type whose value that has been retrieved will be now available for garbage collection.
Key Differences Between Boxing and Unboxing
- In boxing, an object is made to refer as a value type. On the other hand, the process of retrieving the value back from the boxed object is called unboxing.
- A value type stored on a stack is copied to the object stored on heap memory. On the other hand, in unboxing, an object stored on heap memory is copied to a value type stored on stack memory.
- Boxing is an implicit conversion whereas, unboxing is an explicit conversion.
Both the boxing and unboxing consume more time and memory, and they are computationally expensive. They also lack in type safety and increases runtime overhead. It is always advised to avoid too much use of boxing and unboxing in the program.