Search code examples
.netvalue-typereference-type

Why are Value Types created on the Stack and Reference Types created on the Heap?


Programming language books usually explain that value types are created on the stack, and reference types are created on the heap.

My question is Why.


Solution

  • My question is Why.

    Why do they "explain" that? Because sometimes the authors don't know any better themselves, and sometimes they're too lazy to explain it properly. The truth it rather more complicated.

    Fortunately, Eric Lippert has written extensively around this:

    While I would usually try to at least include a paraphrase of the full answer here, I'm not going to do so here - because explaining it very briefly is almost bound to give a misleading result. Just go and read those blog posts - and then everything else Eric has written. It'll take weeks, but you'll be glad you did.