3

I read a thread (which had the input of Marc Gravell and Jon Skeet) that mentioned if a struct is in a class, that struct is stored on the heap like the class.

What if a class is a member of a struct? Where does the class get assigned to?

Thanks

2
  • For info - there are also a number of ways that a struct local-variable (normally on the stack) ends on the heap; captured variables, iterator blocks, etc. Commented Mar 14, 2009 at 23:02
  • Thanks for this. This was actually mentioned by a member in the thread I mention above (I think struct vs class in C# it was called). Commented Mar 14, 2009 at 23:04

2 Answers 2

12

If a class is a member of a struct, then only the reference is stored in the struct (i.e. the 32/64-bit address to the instance). The instance of a class is always on the heap.

Sign up to request clarification or add additional context in comments.

Comments

3

Still on the heap. The struct will only contain a reference to the class instance (like a pointer).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.