What is Boxing and Unboxing in C# ?Intermediate
Memory is divided into the Stack and the heap. Boxing happens when a value type is allocated on the heap. while Unboxing is getting it back into the stack.
What is Boxing ?
Memory is divided into two areas, the Stack and the Heap. Value objects ( e.g primitive types ) are stored on the stack, while reference types ( class instances ) are stored on the Heap.
When Boxing Happens ?
Boxing happens when a value type is allocated on the heap. while Unboxing is getting it back into the stack.
for example : an int type in C# is a primitive value type, it's store on the stack, while "Object" type is the base class where all other objects and types inherit from, it's stored on the heap.
Think about the following scenario :
first declaration int x = 3 is a normal stack allocation while we preserve a variable called "x" with a value of "3", the second statement is casting the "x" into another variable of type "object" called "y" and since all types and objects in C# can cast to the "object" type then this is a legal statement in C#.
The last statement is casting back the "y" variable to an int and save its value in "z" variable.
The first statement is a stack allocation, The second is a heap allocation, The third is another stack allocation.
You can understand what happens with the next figure.
Is Boxing good ?
Boxing sometimes is necessary but it should be avoided when possible. To know why lets first define some concepts in .NET framework.
what is Garbage Collection ?
Garbage Collection is the process of automatically handling memory management to a piece of software called "garbage collector" which takes care of unused and unneeded objects in memory and cleans the house for us.
What is the benefit of Garbage Collector ?
garbage collector removes the overhead of having to manually manage the memory used by the program , making the developer not to worry about how much memory is needed for the program, will be there a free amount of memory available when needed ? Did I clean the memory well after exiting a method execution ?
All these concerns and more are now no longer exist in the era of garbage collector. It allows the developers to focus more on business needs.
Should I worry about memory management when using Garbage Collector ?
As we stated above, garbage collector removed the overhead of worrying about memory management from the developer. But, a good developer understands how things work under the hood, and always code for better performance when possible.
Garbage collector removes object types from the heap when they are no longer needed or used.
Back to the boxing topic, casting the variable to "object" makes it stored on the heap, leaving the garbage collector with more work to do, as it will clean the heap from this unused variable.
How to avoid Boxing ?
Boxing sometimes necessary but it should be avoided at all if possible as it will slow down performance, 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.
one of the methods it can be avoided with , is by using parameterized classes and methods which is implemented using generics in C#.
for example :
The type is determined at compile time. This will improve the performance and prevent the creation of an object on the heap and saves some effort from garbage collector.