boxing

T

Tony Johansson

Hi!

So if I do these two boxxing
int i = 1;
object o1 = i;
MyStruct myStruct = new MyStruct();
Object o2 = myStruct;

These value types int and a struct get boxed but I wonder will these value
types get wrapped in some other
..NET object or will these just be created on the heap as is ?

//Tony
 
S

Scott M.

Tony Johansson said:
Hi!

So if I do these two boxxing
int i = 1;
object o1 = i;
MyStruct myStruct = new MyStruct();
Object o2 = myStruct;

These value types int and a struct get boxed but I wonder will these value
types get wrapped in some other
.NET object or will these just be created on the heap as is ?

//Tony

To be "boxed" means to take a value and place it into a new object created
specifically to hold it. With your code, you are creating a System.Object
and giving it a pointer of "o1". your int value of 1 is being placed into
o1. The opposite is happening with your structure code.

-Scott
 
P

Peter Duniho

Tony said:
Hi!

So if I do these two boxxing
int i = 1;
object o1 = i;
MyStruct myStruct = new MyStruct();
Object o2 = myStruct;

These value types int and a struct get boxed but I wonder will these value
types get wrapped in some other
..NET object or will these just be created on the heap as is ?

What do you mean by "as-is"? If you're asking, for the first example,
whether the 4-byte value representing the number "1" is stored in the
heap without any other memory requirements, the answer is no.

In both cases, the data on the heap is not just the data for the value
type itself, but also a full wrapper that allows for things like virtual
calls and type reflection. The point of boxing is not just to move data
to the heap, but also to provide full reference type semantics for the
object, which wouldn't be possible if just the data were copied there.

Pete
 

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

Top