Skip to content

Value and Reference Storage

Question

Where in memory are reference and value types stored?

Short interview answer

Do not use “value types are on the stack and reference types are on the heap” as a rule. Value types store their data inline wherever their containing storage is; reference-type variables store references, while their objects are commonly allocated on the managed heap.

Detailed answer

A local value type can be stack allocated, but value-type fields are stored inside their containing object, and value types can be boxed. A reference-type variable may itself be a local or a field; it holds a reference to an object. The meaningful language-level distinction is value versus reference semantics, not one fixed physical memory location.

Sources