Possible Duplicates:
Why do we need boxing and unboxing in C#?
What is boxing and unboxing and what are the trade offs?
In C# what does "Box and Unbox" mean?
Here’s an extract from MSDN where I found the text.
But this convenience comes at a cost.
Any reference or value type that is
added to an ArrayList is implicitly
upcast to Object. If the items are
value types, they must be boxed when
they are added to the list, and
unboxed when they are retrieved. Both
the casting and the boxing and
unboxing operations decrease
performance; the effect of boxing and
unboxing can be very significant in
scenarios where you must iterate over
large collections.
Here is a more detailed explanation that looks at the internal of Common Language Runtime.
First, let’s make the difference between value types and reference types:
If you don’t know what the stack is (don’t be offended), it’s a memory area that holds local variables in a method and addresses of caller functions used for
returninstruction (just to be brief and provide a general answer). When you call a method, a sufficient area on the stack is statically allocated to it, so stack allocation is always called static allocation.The heap, instead, is a memory area separated from the stack, property of the running process, in which allocation must be first demanded to the operating system, and that’s why it’s called dynamic allocation (if you don’t run in an if statement, for example, memory may not be allocated for your process, instead stack is always allocated).
Just to make a final example on heap and stack: in languages such as C++, declaring
int[100] a;statically allocates 100*8 bytes on the stack (64-bit system assumed), whileint* a = new int[100];declares a 8 bytes (on 64-bit systems) area on the stack AND requests 800 more bytes on the heap, if and where available.Now let’s talk about C#:
Boxing
Since int is a value type, and is allocated on the stack, when you cast it to object or any other reference type (actually there is no other reference type from which int can inherit, but it’s a general rule) the value must become necessarily a reference type. So a new area on the heap is allocated, the object is boxed inside it and the stack holds a pointer to it.
Unboxing
Just the opposite: when you have a reference type, such as object, and want to cast it to a value type, such as to int, the new value must be held on the stack, so CLR goes to heap, un-boxes the value and copies it to the stack.
In other words
Remember the
int[]andint*examples? Simply, when you haveintin C#, the runtime expects its stack location to hold the value but instead when you haveobject, it expects its real value to be in the heap location pointed by the stack.