3

Could anybody please explain what is actually happening behind the scenes when we perform boxing and unboxing?? I know boxing is conversion of value to reference type and unboxing is the reverse, but behind the scenes while boxing is that the boxed variables actually gets stored in heap and what is the basic use of boxing and unboxing?

Thanks!

Archana
  • 99
  • 13
  • possible duplicate of [What is boxing and unboxing and what are the trade offs?](http://stackoverflow.com/questions/13055/what-is-boxing-and-unboxing-and-what-are-the-trade-offs) – CD.. Jun 16 '11 at 11:21
  • You can refer this [Post](http://stackoverflow.com/questions/2111857/why-do-we-need-boxing-and-unboxing-in-c) ! – Bibhu Jun 16 '11 at 11:21
  • Please follow [this](http://stackoverflow.com/questions/13055/what-is-boxing-and-unboxing-and-what-are-the-trade-offs), [this](http://stackoverflow.com/questions/4015529/boxing-and-unboxing) and [this](http://stackoverflow.com/questions/764216/boxing-vs-unboxing) – crypted Jun 16 '11 at 11:18
  • Its not a good idea to refer to a link without any description, what if the link you provided gets broken? – FIre Panda Jun 16 '11 at 11:27
  • Did you try any of those ? – crypted Jun 16 '11 at 11:45
  • Yeah I tried the links you posted, but its a better idea to provide some desc too, bec if the link gets broken in future, you answer wouldn't be of any use – FIre Panda Jun 16 '11 at 11:48
  • This [MSDN article](http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx) explains it perfectly – Felipe Sabino Jun 16 '11 at 11:20

3 Answers3

3

There is no magic at all, just keep it simple as is...
Boxing is the act of converting a value-type instance to a reference type instance.
Unboxing reverses the operation by casting the object(reference type) back to the original value type.

So you have to understand difference between value-type and reference-type and also stack and heap

Value types - build-in types like int, string, char, double and struct - stored in a block of memory called STACK
Reference type - class, delegate, object - stored in a block of memory called HEAP

Stack vs Heap

Now, when you understend the diagram above let's take a look on the real, simple code.

    int i = 1;       
    object O = i;         // Box the int
    int j = (int)O;       // Unbox the int

enter image description here

caveman_dick
  • 6,302
  • 3
  • 34
  • 49
Serge V.
  • 3,377
  • 3
  • 20
  • 28
2

According to MSDN:

Boxing and unboxing enable value types to be treated as objects. Boxing a value type packages it inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap. Unboxing extracts the value type from the object.

[...]

In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, an entirely new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
0

I found that the following articles are very helpful and informative:

Community
  • 1
  • 1
foxy
  • 7,599
  • 2
  • 30
  • 34