Awarious OfficePortal - HRMS Application

What is boxing and Unboxing in C#?

0 votes
asked Jul 2, 2013 in C#.NET by anonymous

What is the concept of boxing and unboxing in C#? How does it really affect the performance of the code execution? How do I resolve this performance issue?


1 Answer

0 votes
answered Jul 2, 2013 by Aadhira (1,215 points)
selected Jul 3, 2013 by administrator
Best answer

What is the concept of boxing and unboxing in C#? How does it really affect the performance of the code execution? How do I resolve this performance issue?
There are two kinds of variables we can have in C# programming, called primitive type variables (store in stack) and non-primitive type (reference type which are stored in heap) variables.

Primitive variable: int x = 0;

Reference type: object temp = new object();

Primitive type variable are basically value type which cannot be set to null. But non-primitive types are reference type and they can be set to null because they are stored in heap memory.
If you change the primitive type variables to reference type variable, then it is called boxing. The reverse action (reference type to primitive) is called unboxing.

Boxing: int x = 10;
object temp = new object();
temp = x;

Basically what we are doing here is, take a primitive variable and put it inside an object. So, the primitive value now stored in heap memory. So, we can change it to null of we can change the value by passing it as reference object etc.

But the problem with this is, whenever we want to read the value inside that “temp” object, we need to do type casting

Type casting is a concept of converting one variable type to another variable type. In our example, we need to convert the variable “temp” which the type of “object” to “int” type

Syntax for type cast in C# to unbox:

The above code used to unbox the object type to int type. This may look very simple action to do. But when we are dealing with millions of actions like this will drop the performance of the application.

Another boxing example is, whenever any strongly typed objects are stored inside the non strongly typed object i.e “Object” type, then that also can be considered as Boxing, because that also involves type casting.

Best example for this is to using ArrayList. Because ArrayList is nothing but a collection of objects and it can store any kind of value inside. So, whenever we need to read value out of it, we need to unboxing by implementing type casting.
So, always it is better to use strongly typed variables. In case if you want to use collection of objects, then go for the Generic collections like

HashSet<T>, List<T>, Stack<T>, Queue<T> where ‘T’ is nothing but the type you want to store.

List<int> integerCollection = new List<int>(); 

This above collection will allow me to store only the integer values. So, there is no need to do type cast. No need for boxing and unboxing.

If you want to understand about generics, please take a look at the below link

Your answer


Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2019