Where are value types and reference types stored?
Short answer
Local value types typically live on the stack; reference-type objects live on the managed heap with the reference on the stack. Value-type fields inside a class live on the heap with the object.
Advertisement
AModel answer
Local value types typically live on the stack; reference-type objects live on the managed heap with the reference on the stack. Value-type fields inside a class live on the heap with the object.
class Box { public int N; } // N lives on the heap inside Box
int local = 5; // stackReal-world example
Understanding this explains why large structs are costly to copy.