What is boxing and unboxing in C#?
Data types in C# can be typically classified into two types viz. value types and reference types. The reference type saves the address of the value where it is stored, but the value type keeps the item itself.
Value types include int, float, double, decimal, bool, char, and others, while reference types include object, string, and array. You'll frequently need to convert value types to reference types or vice versa while working with these data types.
Because they have different features and are stored differently in memory,. NET must perform some internal work to convert them from one type to the other. Boxing and unboxing are the tools for these conversion procedures. So let us quickly understand what is boxing and unboxing in C#.
Table of contents
- What is Boxing in C#?
- Example of Boxing in C#
- Why is this concept named Boxing?
- What is Unboxing in C#?
- Example of Unboxing in C#
- Key Differences between Boxing and Unboxing in C#
- Conclusion
What is Boxing in C#?
Boxing is the implicit type of conversion transforming a value type to a reference type. It involves conversion of a value type to a type object or any interface type implemented by this value type. The CLR engulfs a value type in a Object instance and saves it on the managed heap when it boxes it.
In the garbage-collected heap, boxing is employed to store value types. When you box a value type, it creates a new object instance on the heap and copies the value into it.
Here’s a simple example:
Declare a value type variable (num) of integer type and assign any value to it. Create an object reference type (obj) and apply the Explicit operation, which causes the num value type to be copied and saved in the object reference type obj, as illustrated below:
Example of Boxing in C#
Following is the program to depict boxing in C#:
Output:
Why is this concept named Boxing?
As you may be well aware, all reference types saved on the heap contain the value's address. The value type is just an actual value placed on the stack. Int I is now allocated to object o (as seen in the first example). Object o must be an address rather than a value. As a result, the CLR creates a new System to box the value type. The entire process is called 'Boxing' because the CLR constructs a box on the heap that stores the item.
What is Unboxing in C#?
As the name suggests, unboxing is the exact opposite of boxing. It is the transformation of a reference type to a value type. The value from the reference type is extracted and assigned to a value type via unboxing. The act of unboxing is explicit. This necessitates casting specifically.
In other words, explicit conversion from a type object to a value type, or from an interface type to a value type that implements the interface, is known as unboxing. An unpacking operation entails the following steps:
- Ensuring whether the object instance is a boxed value of the specified value type.
- The value of the instance is copied into the value-type variable.
Here’s a simple example:
Declare a value type variable (num) of the integer type, storing a value. Make a reference object type now (obj). The explicit boxing process creates an integer value of type I and uses the casting method. The referenced type on Heap is then copied to stack, as seen in the diagram below:
Example of Unboxing in C#
Following is the example of unboxing in C#:
Output:
Key differences between Boxing and Unboxing in C#
Boxing
- It is used to transform a value type to an object type
- Boxing is a form of implicit conversion
- The value on the stack is copied to the object on the heap memory
Example of boxing-
// C# program to illustrate Boxing
using System;
public class example {
static public void Main()
{
int value = 20;
// Boxing
object obj = value;
// Change the value of val
value = 30;
Console.WriteLine("Value type of value is {0}", value);
Console.WriteLine("Object type of value is {0}", obj);
}
}
Output
Unboxing
- It converts a object type into an value type
- Unboxing is a form of explicit conversion
- The item from the heap memory is copied to the value from the stack memory
Example of unboxing in C#
// C# program to illustrate Unboxing
using System;
public class example {
static public void Main()
{
int value = 20;
// Boxing
object obj = value;
// Unboxing
int a = (int)obj;
Console.WriteLine("Value of object is {0}", obj);
Console.WriteLine("Value of a is {0}", a);
}
}
Output
Conclusion
Think of it this way - Int values are just 32-bit numbers for applications that don't require int values to operate as objects. The ability to treat value types as objects bridges the gap that exists in most languages between value types and reference types. This functionality is accessible on-demand for programs that requires int values to behave like objects.