Do Value Types in C# always go on the stack?

It is quite well-known that value-types are created on the Stack (sometimes in registers as well depending on the allocation strategy) and reference types are based in the Heap area of memory. But when you are creating a custom type and you wish to embed value types in there, where would they reside?

So it depends on the context. If a value type is being created as a local or rather temporary variable in a function, then they will always lie in the stack or registers otherwise they will reside where they are created – which in this case happens to be the heap.

For more information, please refer – http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx

leave your comment