0
0
C Sharp (C#)programming~10 mins

Stack vs heap mental model in C Sharp (C#) - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Stack vs heap mental model
Start: Program runs
Function call: push stack frame
Allocate local variables on stack
If new object created
Allocate object on heap
Store reference on stack
No
Function returns: pop stack frame
Heap objects remain until GC cleans
End
This flow shows how function calls create stack frames with local variables, while objects are allocated on the heap with references stored on the stack.
Execution Sample
C Sharp (C#)
void Foo() {
  int x = 10;
  Person p = new Person();
  p.Name = "Alice";
}

Foo();
This code calls Foo, which stores an int on the stack and creates a Person object on the heap with a reference on the stack.
Execution Table
StepActionStack StateHeap StateNotes
1Call Foo()Push Foo stack frameEmptyStack frame for Foo created
2int x = 10;x=10EmptyLocal int stored on stack
3Person p = new Person();x=10, p=ref1Object Person#1 allocatedPerson object created on heap, reference stored in p
4p.Name = "Alice";x=10, p=ref1Person#1.Name = "Alice"Object field set on heap
5Return from Foo()Pop Foo stack framePerson#1 remainsStack frame removed, heap object still exists
6EndEmptyPerson#1 remains until GCHeap object persists until garbage collected
💡 Function returns, stack frame is popped, heap objects remain until garbage collection
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
xundefined101010undefinedundefined
pundefinedundefinedref1ref1undefinedundefined
Heap Person#1nonenoneallocatedName="Alice"allocatedallocated
Key Moments - 3 Insights
Why does the local variable 'p' store a reference and not the actual object?
Because objects are stored on the heap due to their dynamic size and lifetime, the stack only holds a reference (pointer) to that heap object, as shown in execution_table step 3.
What happens to the heap object after the function returns?
The stack frame is popped and local variables are removed, but the heap object remains until garbage collection, as seen in execution_table steps 5 and 6.
Why are value types like 'int x' stored on the stack?
Value types have fixed size and short lifetime tied to the function call, so they are stored directly on the stack for fast access, as shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3. What does variable 'p' hold?
ANull
BA reference to the Person object on the heap
CThe actual Person object data
DAn integer value
💡 Hint
Check the 'Stack State' and 'Heap State' columns at step 3 in the execution_table.
At which step does the stack frame for Foo get removed?
AStep 5
BStep 2
CStep 4
DStep 6
💡 Hint
Look for 'Pop Foo stack frame' in the 'Action' column of the execution_table.
If 'p' was a value type instead of a reference type, how would the stack state change at step 3?
A'p' would be null
B'p' would hold a reference to heap object
C'p' would hold the actual object data on the stack
DHeap would have no objects
💡 Hint
Refer to variable_tracker and key_moments about value types stored on stack.
Concept Snapshot
Stack vs Heap Mental Model in C#:
- Stack stores local variables and function call info.
- Heap stores objects created with 'new'.
- Stack variables hold values or references.
- Heap objects live beyond function calls until GC.
- Stack is fast and short-lived; heap is slower and long-lived.
Full Transcript
When a C# program runs and calls a function like Foo, a stack frame is created to hold local variables such as integers and references. Value types like int are stored directly on the stack. When the code creates a new object with 'new', the object is allocated on the heap, and the stack stores a reference to it. The heap object can have fields, like Name, which are stored inside the heap. When the function returns, the stack frame is removed, so local variables disappear, but the heap object remains until garbage collection. This model helps understand memory management and variable lifetimes in C#.