In this article, we will learn about Boxing and Unboxing in C#.
Introduction
Boxing
Unboxing
Performance implication
Boxing and unboxing are one of those concepts which every .NET developer should understand. Lot of time knowingly and unknowingly we implement these concepts and it can have disastrous performance issues. So in this article we see what is boxing and unboxing and why does it cause performance issues and how to use them carefully.
So let us start with boxing and below are some points and definitions around it.
Boxing is a process of converting value type to the referenced type.Value type is stored in Stack MemoryReferenced Type is stored in Heap Memory.
Let us understand the above statement in detail.
Below is a simple console application code which does Boxing.
static void Main(string[] args)
{
int i = 10; // Step 1 Declar/Initialisation of Variable i of integer datatype.
object o; // Step 2 :- Declaration of Object o.
o = i; // ß Step 3 :- Boxing happens here .Assigning value of i to object o.
}
When the above programs runs below are the sequence of events :-
First the variable ‘i’ is assigned 10 value and it is stored on Stack memory.Then object reference ‘o’ is stored on the Heap.In the third step boxing happens where value type is moved to the object and in this case the value is assigned on heap.
Unboxing is a process of converting reference type to the value type.
Let us understand the above statement in detail.
Below is a simple code which does Unboxing.
static void Main(string[] args)
{
int i = 10; // Declaration and Initialisation of Variable i of integer datatype.
object o; // Declaration of Object o.
o = i; // Boxing
int j = (int)o; // Unboxing
}
During Unboxing the referenced type residing on Heap Memory is copied to Stack Memory as a value type. Also Unboxing checks the referenced type to make sure that it is a boxed value of the given value type. It means before unboxing ‘o’ it will check whether ‘o’ is a referenced type and is pointing to a value in heap memory.
So now that we have understood the definition of boxing and unboxing, the next thing is to understand what is the implication of boxing and unboxing. Boxing and unboxing causes your variables to jump from one memory type (value type) to other memory type (reference type) which can further lead to performance issues.
So in simple word, we should try to avoid boxing and unboxing as much as we can.
But then there are situations where we can not avoid it. For example see the below code. We are taking data from the user interface (web page)I. When we receive data from user interface it comes in the form of string so we need to unbox to convert it into the data type we want. And also when we set data the data to user interface objects like TextBox, we need to convert to string.
int I = Convert.ToInt(textbox1.text);
Below is video which explains the concept of boxing and unboxing and it also demonstrates a small performance testing highlighting the performance implications of boxing and unboxing.