C# in Depth

Cover of C# in Depth
Order now (3rd edition)

Stacks, heaps, and caring too much

Chapter 5: Fast-tracked delegates: 5.5.4

Created: 2/24/2008
Last updated: 2/24/2008

Remember the section in chapter 2 where I mention that in some ways managed developers shouldn't care about whether things are placed on the stack or the heap? Well, you've got Eric to thank for that. I've just always cared by default.

This section about closures shows a good reason why it's sometimes not worth caring - you end up being less surprised when things move around unexpectedly. Unsurprisingly, Eric puts it best:

The whole point of managed memory is that every object lives at least as long as it needs to. The idea that "local stuff vanishes" has nothing to do with whether the implementation is "on the stack" or not – stacks are a means to an end, not an end in themselves.

It had never occurred to me to think in such a liberating, non-implementation-specific way before writing this book. Since reading Eric's comments, I've been coming up with all kinds of bizarre ideas, many of which are completely unworkable - but it's a valuable experience nonetheless.