469,644 Members | 1,961 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,644 developers. It's quick & easy.

What happens to a reference type in a valuetype struct?

I was just curious. I know that a struct is value type by definition,
and will be placed on the stack, but what if you have a reference type
in the struct, say a string, or an array. Where will that be placed?

Oct 23 '06 #1
3 7973
I was just curious. I know that a struct is value type by definition,
and will be placed on the stack, but what if you have a reference type
in the struct, say a string, or an array. Where will that be placed?
The reference type is actually allocated on the heap but the reference is
on the stack (in the value type) -- assuming that your value type is on the
stack.

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 23 '06 #2

DaTurk wrote:
I was just curious. I know that a struct is value type by definition,
and will be placed on the stack, but what if you have a reference type
in the struct, say a string, or an array. Where will that be placed?
The value type will be big enough to accommodate a reference (pointer)
to the reference type (plus anything else in the value type). The
storage for the state (fields) of the reference type itself will always
be on the heap.

By the way, this type of construct is possible, but it's usually best
that the reference type in question be immutable. If the state (fields,
contents) of the reference type can be changed then the value type will
act oddly, to say the least.

I do just what you described to implement a Measure: a quantity coupled
with a unit of measure (for example 5 combined with UnitOfMeasure.Feet
to represent five feet). The Measure is a struct, but the UnitOfMeasure
is a class. The important point is that the UnitOfMeasure class has no
properties that can be set, and its methods never change it: once it's
constructed it's immutable. This keeps the Measure struct acting in
reasonable ways.

Oct 23 '06 #3
DaTurk <mm******@hotmail.comwrote:
I was just curious. I know that a struct is value type by definition,
and will be placed on the stack
No, not necessarily. A value type is stored in the same context as it
is declared: if it's a member of a reference type, for instance, it
will be on the heap, as part of the data for the object.
but what if you have a reference type
in the struct, say a string, or an array. Where will that be placed?
The reference is placed in the same context as the rest of the data for
the struct. The object that the reference refers to will certainly be
on the heap.

See http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

51 posts views Thread by jacob navia | last post: by
43 posts views Thread by Mountain Bikn' Guy | last post: by
59 posts views Thread by peter.tornqvist | last post: by
70 posts views Thread by garyusenet | last post: by
1 post views Thread by Anonymous | last post: by
6 posts views Thread by kbin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.