Boxing

C

cody

What is Boxing & UnBoxing in .NET?

implicit conversion from reference type (class,interface) to a value type
(struct, primitive data types like int) is called boxing:

object obj = 1234;

unboxing is to convert the boxed struct back to its original form:

int i = (int)obj;

boxing and unboxing is a relatively expensive operation and should be
avoided if you have performance problems.
 
W

William Ryan eMVP

Lora, with all due respect, questions like that are very general and it's
hard to tell, on this end, how to answer. One could give you a very generic
answer that won't necessarily tell you what you want to know if you are a
relatively experienced programmer new to .NET. If you are a total newbie,
many answers may be over your head. As such, if you gave just a little
more detial "I know the distinction between Reference and Value types, but
can some explain how the pertain to boxing" or "I'm a total newbie to .NET,
can someone explain what Boxing is and why it matters".

This may sound like I'm being petty, but if I knew that you knew the
distinction between reference and value types, I'd give you a totally
different answer than if I knew you were a total newbie. If you ask a
little more specific question, you'll probably get a better answer ;-)
http://www.knowdotnet.com/articles/boxing.html
http://www.csharphelp.com/archives/archive100.html
 
C

Cor Ligthert

Hi Lara,

When you put someting in a box it is boxing, when you get it out, it is
unboxing.
When you put a value in an object it is boxing, when you get it out, it is
unboxing.

Cor
 
S

Scott M.

William, you are right that more info. is always better. But this one
doesn't require that much additional explanation. If the OP needs more,
they can re-post.

To the OP:

In .NET objects are either stored in the memory "stack" or the memory
"heap". Value types (objects that mainly store a data value) are placed on
the stack, while reference types (more complex objects that may have values,
but also have behaviors - classes) are stored on the heap.

When you convert a reference type to a value type, for example: CType("6",
Integer), you cause the object to change where it is stored. It must now be
taken off the heap and placed on the stack. This operation has overhead and
so, should only be done when necessary.
 
W

William Ryan eMVP

Are you sure they know what the difference between a value and a reference
type is? I wasn't and if you don't know that distinction, any meaningful
explanation of boxing is not going to be very doable.
 
J

Jon Skeet [C# MVP]

Scott M. said:
In .NET objects are either stored in the memory "stack" or the memory
"heap". Value types (objects that mainly store a data value) are placed on
the stack, while reference types (more complex objects that may have values,
but also have behaviors - classes) are stored on the heap.

That's already incorrect though - value types also end up on the heap,
unless they're local variables:

http://www.pobox.com/~skeet/csharp/memory.html
When you convert a reference type to a value type, for example: CType("6",
Integer), you cause the object to change where it is stored. It must now be
taken off the heap and placed on the stack. This operation has overhead and
so, should only be done when necessary.

Again, the unboxed value may be on the heap rather than the stack.
 
C

Cor Ligthert

A wonder a wonder, I wrote something about Boxing and no comments from Jon.

:))

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Boxing/unboxing 2
Boxing m.m 2
about performance 2
Why boxing? 1
Boxing & Unboxing F1 F1 F1... 6
Type Conversion 4
What would be a faster alternative to boxing/unboxing? 43
Unboxing simple types 2

Top