Where are ref types that are members of value types stored?

  • Thread starter Thread starter Sathyaish
  • Start date Start date
S

Sathyaish

Structs are value types, strings are ref types. If you have something
like this:

struct foo
{

System.String str;
}

foo TheDayToday;

then, where's

TheDayToday.str

going to be stored?

(a) The string is going to be stored on the stack;
(b) The struct is going to be stored on the stack with a 32-bit
System.IntPtr/System.UInt32 also copied on the stack holding the
address/reference to the actual string allocated on the heap.
 
(b) is the case. The reference object is allocated on the managed
heap, as usual. The struct contains the reference to that object,
just as if you had created a local variable to a String.
 
Thanks, Chris. Are you the same person that I see on JoS? :-)

I am. And you are the same Sathyaish? I didn't recognize you,
probably due to the unexpected brevity of your post! :-p
 
Back
Top