Skip to content

Module 3: C# Core & Memory (The Workbench)

📚 Module 3: C# Core & Memory

Course ID: DOTNET-101
Subject: The Workbench of Types

In professional .NET development, we don’t just “store data.” We manage Memory. To build fast apps, you must understand the difference between the Stack and the Heap.


🏗️ Step 1: Value Types (The “Post-it Note”)

Value Types (like int, bool, double, and struct) are stored on the Stack.

🧩 The Analogy: The Post-it Note

  • When you write a phone number on a Post-it Note, you are holding the actual data.
  • If you give a copy of that note to a friend, they have their own piece of paper. If they change the number on their note, yours stays the same.
  • The Stack: Lightning fast, but small. Cleaned up instantly when the function ends.

🏗️ Step 2: Reference Types (The “Library Card”)

Reference Types (like class, string, and array) are stored on the Heap.

🧩 The Analogy: The Library Card

  • You don’t carry the whole library in your pocket. You carry a tiny Library Card (The Reference/Pointer).
  • The actual books (The Data) live in a giant warehouse (The Heap).
  • If you give your card to a friend, you both point to the same book. If they rip a page out, you see the damage too!

🏗️ Step 3: Why does this matter?

  1. Performance: Accessing the Stack (Post-it) is 100x faster than finding a book in the Heap warehouse.
  2. Bugs: Beginners often accidentally change data because they don’t realize two variables are “Pointing” to the same object on the Heap.

🧪 Step 4: Python Dev’s Guide to Memory

In Python, everything is a Reference Type (on the Heap). In C#, you get to choose!

  • Use a struct (Stack) for small things like a Point(x, y).
  • Use a class (Heap) for large things like a Customer or Order.

🥅 Module 3 Review

  1. The Stack: Fast memory for “Post-it” value types.
  2. The Heap: Large memory for “Library” reference types.
  3. Value Type: Copying the note creates a new note.
  4. Reference Type: Copying the card points to the same object.