Member-only story
The Heap Is Not the Stack
What actually happens when C# creates values, objects, references, and local variables
One of the first explanations that took me beneath the surface of C# looked something like this.
STACK HEAPint age = 42 Person object
bool active = true List object
reference to customer --> Customer objectIt’s wonderfully easy to remember, and tremendously useful, because it introduces a distinction that C# syntax can make surprisingly easy to miss.
It comes down to understanding that a variable can hold the thing itself, or it can hold a way to get to the thing.
Imagine we are building a game. Somewhere in that game is a player called Ada. She has 100 health points and a sword.
player1 ─────┐
│
▼
PLAYER OBJECT
┌───────────────┐
│ Name: Ada │
│ Health: 100 │
│ Weapon: Sword │
└───────────────┘The variable player1 is not Ada. It contains a reference that lets our code reach Ada's Player object.
Now we do this.
Player player2 = player1;






