Why boxing?

S

Sharon

I’m wondering about the .NET boxing…
All value types and all reference type are derived from System.object
But value type are allocated in the stack and the reference type are
allocated in the heap.
The boxing `warps` the value type in a new allocated object in the heap.
So far is what all known !
(http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx)

But; it the object is a reference type (otherwise it won’t be allocated for
the boxing in the heap), and this object is the base class of the value type.
Why the need for the boxing? How come these value type are allocated on the
stack?

I’m confused 0-:

Does anybody can shed some light on this issue?
 
P

Peter Duniho

Sharon said:
I’m wondering about the .NET boxing…
All value types and all reference type are derived from System.object
True.

But value type are allocated in the stack and the reference type are
allocated in the heap.

False. Value types are allocated where they are declared. If they are
local variables, they are allocated on the stack. But if they are class
members, they are allocated elsewhere. Instance members that are value
types are on the heap with the rest of the class object, and static
members are wherever the run-time decides to put static members (which
could be the heap, but is definitely not the stack).
The boxing `warps` the value type in a new allocated object in the heap.
True.

So far is what all known !
(http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx)

But; it the object is a reference type (otherwise it won’t be allocated for
the boxing in the heap), and this object is the base class of the value type.

What is that sentence supposed to mean? System.Object is the base type
for all types. It's a class, and so is a reference type, which is
exactly why when you are treating a value type instance as System.Object
it _has_ to be boxed. Otherwise, you wouldn't have a reference type.
Why the need for the boxing? How come these value type are allocated on the
stack?

Don't think about the stack. It's misleading and is only tangentially
related to the way value types works.

The real difference between value types and reference types is that
values types are always _copied_ whole on assignment or other
referencing, while with reference types, only the reference to the
object is copied, not the entire object.

This provides a useful difference in semantics, so that you can use the
kind of type best suited to a particular problem. If you need to be
able to share a reference to the same object in multiple places in code,
then a reference type works best. On the other hand, if you want to be
able to pass a copy of your object to some other code without it being
able to affect the value you're using locally, a value type works best.

Value types can also be helpful for performance, as when allocated they
are always allocated implicitly as part of some other object. For
example, as elements in an array, or as a data member in some other
type. This feature allows you to avoid having to allocate numerous
objects just for the purpose of storing what is essentially a single object.

Note that all of the primitive types — int, bool, float, etc. — are
value types. It can be helpful to consider those examples when thinking
about user-defined value types. A user-defined value type should be
used in the same kinds of situations when these other primitive types
would be used, but when you need some kind of data or behavior that the
primitive types don't provide for.

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