C# – Storage of Value Types and Reference Types in .NET

cnet

In .net, the value types are stored on stack where as reference types are stored on managed heap. What is the reason for this one? Is it not possible to exchange their storage locations?

Best Answer

The stack is an implementation detail. The important thing is the reference/value distinction.

Is it not possible to exchange their storage locations?

It's very easy to store value types on the heap.

class ReferenceInt
{
    public int Field;
}

ReferenceInt n = new ReferenceInt();
// n.Field is now stored on the heap.
Related Topic