📖 Scenario: Imagine you are organizing your workspace. You have a small desk drawer for quick notes (stack) and a big filing cabinet for important documents (heap). You want to understand how C# stores simple data and objects in these two places.
🎯 Goal: You will create a simple C# program that shows how value types (stored on the stack) and reference types (stored on the heap) behave differently. You will see how changing one variable affects another depending on where the data is stored.
📋 What You'll Learn
Create a value type variable and assign it a number
Create a reference type variable (class instance) with a number property
Copy the value type variable to another variable
Copy the reference type variable to another variable
Change the copied variables and observe the effect on the originals
Print the results to show the difference between stack and heap behavior
💡 Why This Matters
🌍 Real World
Understanding stack and heap helps you write efficient programs and avoid bugs related to data changes.
💼 Career
Many programming jobs require knowledge of memory management and how data is stored and accessed.
Progress0 / 4 steps
1
Create a value type variable
Create an int variable called stackNumber and set it to 10.
C Sharp (C#)
Hint
Use int stackNumber = 10; to create the variable.
2
Create a reference type variable
Create a class called HeapNumber with a public int property called Value. Then create an instance called heapNumber and set its Value to 10.
C Sharp (C#)
Hint
Define the class with a public property, then create an object and set its property.
3
Copy variables and change copies
Create a new int variable called stackCopy and set it equal to stackNumber. Create a new HeapNumber variable called heapCopy and set it equal to heapNumber. Then change stackCopy to 20 and heapCopy.Value to 20.
C Sharp (C#)
Hint
Assign the variables and then change the copies as instructed.
4
Print the results
Print the values of stackNumber, stackCopy, heapNumber.Value, and heapCopy.Value using Console.WriteLine to show how stack and heap behave differently.
C Sharp (C#)
Hint
Use Console.WriteLine with string interpolation to print the variable values.
Practice
(1/5)
1. In C#, where are value types like int and bool typically stored during program execution?
easy
A. On the heap, because they are objects that need dynamic memory
B. In static memory, shared across all instances
C. In the CPU cache only, not in stack or heap
D. On the stack, because they are stored directly and accessed quickly
Solution
Step 1: Understand value type storage
Value types like int and bool are stored on the stack because they hold data directly and have a short lifetime.
Step 2: Contrast with heap storage
Heap stores objects (reference types), not simple value types, which makes stack the correct place for these.
Final Answer:
On the stack, because they are stored directly and accessed quickly -> Option D
Quick Check:
Value types = stack [OK]
Hint: Value types go on stack; objects go on heap [OK]
Common Mistakes:
Thinking all data goes on heap
Confusing value types with reference types
Assuming stack is for objects only
2. Which of the following C# declarations correctly creates a reference type stored on the heap?
easy
A. string text = "hello";
B. double pi = 3.14;
C. bool flag = true;
D. int number = 5;
Solution
Step 1: Identify reference types
In C#, string is a reference type stored on the heap, while int, bool, and double are value types stored on the stack.
Step 2: Match declaration to heap storage
Only string text = "hello"; creates a reference type object on the heap.
Final Answer:
string text = "hello"; -> Option A
Quick Check:
Reference type = string [OK]
Hint: Strings are reference types stored on heap [OK]
Common Mistakes:
Assuming all primitives are reference types
Confusing string as value type
Ignoring heap for object storage
3. Consider the following C# code:
class Person { public string Name; } Person p1 = new Person(); p1.Name = "Alice"; Person p2 = p1; p2.Name = "Bob"; Console.WriteLine(p1.Name);
What will be the output and why?
medium
A. "Bob" because p1 and p2 reference the same object on the heap
B. Compilation error due to missing constructor
C. "Alice" because p1 and p2 are separate objects on the heap
D. "Alice" because strings are immutable
Solution
Step 1: Understand reference assignment
Both p1 and p2 point to the same Person object on the heap because p2 = p1; copies the reference.
Step 2: Trace the name change effect
Changing p2.Name to "Bob" changes the same object that p1 references, so p1.Name is also "Bob".
Final Answer:
"Bob" because p1 and p2 reference the same object on the heap -> Option A
Quick Check:
Reference copy = shared object [OK]
Hint: Reference copy means both variables point to same heap object [OK]
Common Mistakes:
Thinking p1 and p2 are independent copies
Confusing value and reference assignment
Ignoring that strings are immutable but object fields can change
4. Identify the error in this C# code snippet related to stack and heap usage: