Got it. Here’s **Phase 1 – Foundations (Lessons 1–5)** of your **C# Masterclass: From Zero to Hero**. Everything below is formatted as ready-to-paste Markdown for Visual Studio Code or Obsidian. Each lesson has objectives, explanations, annotated examples, and a mini-project. When you’ve saved this section, I’ll continue straight to **Phase 2 (Lessons 6–9)**. --- # 🧠 C# MASTERCLASS — PHASE 1 FOUNDATIONS **Environment:** Visual Studio Code + .NET SDK 8 **Tone:** Technical Instructor **Goal:** Learn core syntax, control flow, data handling and debugging. --- ## Lesson 1 – The First Line of C# ### Objectives - Understand the structure of a C# program. - Compile and run a console application in VS Code. - Use `Console.WriteLine()` and `Console.ReadLine()`. ### Concepts A minimal C# console program: ```csharp using System; class Program { static void Main(string[] args) { Console.WriteLine("Hello, Genius!"); } } ``` ### Explanation - `using System;` imports the base namespace that contains `Console`. - `Main()` is the entry point; it must be `static`. - `Console.WriteLine()` writes a line to stdout. ### Exercises 1. Replace the greeting with your own name. 2. Add a `Console.ReadLine()` so the window waits for input. ### Mini-Project — _Hello Genius App_ Ask for the user’s name and greet them. ```csharp Console.Write("Enter your name: "); string name = Console.ReadLine(); Console.WriteLine(quot;Welcome to C#, {name}!"); ``` --- ## Lesson 2 – Variables of Power ### Objectives - Understand data types and declarations. - Perform basic operations and type conversion. ### Core Types |Type|Example|Description| |---|---|---| |`int`|`int x = 5;`|Whole numbers| |`double`|`double y = 2.5;`|Floating-point| |`char`|`'A'`|Single character| |`string`|`"text"`|Sequence of chars| |`bool`|`true/false`|Logical values| ### Arithmetic Example ```csharp int a = 10; int b = 3; double result = (double)a / b; Console.WriteLine(result); // 3.3333 ``` ### Casting & Conversion ```csharp string s = "42"; int value = int.Parse(s); ``` ### Mini-Project — Unit Converter Prompt for kilometres and convert to miles: ```csharp Console.Write("Enter distance in km: "); double km = double.Parse(Console.ReadLine()); double miles = km * 0.621371; Console.WriteLine(quot;{km} km = {miles:F2} miles"); ``` --- ## Lesson 3 – Logic and Flow ### Objectives - Apply `if`, `else if`, `switch`. - Use loops: `for`, `while`, `foreach`. - Combine conditions with logical operators. ### Conditional Example ```csharp Console.Write("Enter age: "); int age = int.Parse(Console.ReadLine()); if (age >= 18) Console.WriteLine("Adult"); else if (age >= 13) Console.WriteLine("Teenager"); else Console.WriteLine("Child"); ``` ### Loop Examples ```csharp for (int i = 1; i <= 5; i++) Console.WriteLine(i); int x = 0; while (x < 5) { Console.WriteLine(x); x++; } ``` ### Mini-Project — Number Guessing Game ```csharp Random rnd = new Random(); int target = rnd.Next(1, 21); int guess = 0; Console.WriteLine("Guess a number 1-20:"); while (guess != target) { guess = int.Parse(Console.ReadLine()); if (guess < target) Console.WriteLine("Too low!"); else if (guess > target) Console.WriteLine("Too high!"); else Console.WriteLine("Correct!"); } ``` --- ## Lesson 4 – Debugging Like a Detective ### Objectives - Use breakpoints and step-through debugging in VS Code. - Inspect variables and call stack. - Understand common runtime errors. ### Tools 1. **Set Breakpoint:** Click left of line numbers. 2. **Run Debug:** `F5` → launch configuration `.vscode/launch.json`. 3. **Watch Variables:** Check values while stepping. ### Common Errors |Error|Cause|Fix| |---|---|---| |`NullReferenceException`|Using a null object|Initialise before use| |`FormatException`|Bad input to `Parse()`|Validate input| |`IndexOutOfRangeException`|Invalid array index|Check bounds| ### Mini-Project — Bug Hunt Broken code: ```csharp string[] names = null; for (int i = 0; i <= names.Length; i++) Console.WriteLine(names[i]); ``` **Fix:** ```csharp string[] names = { "Ana", "Ben", "Cara" }; for (int i = 0; i < names.Length; i++) Console.WriteLine(names[i]); ``` Run under debugger and observe each iteration. --- ## Lesson 5 – Collections and LINQ ### Objectives - Work with arrays and generic collections. - Understand basic LINQ queries. - Build a small in-memory data manager. ### Arrays ```csharp int[] scores = { 90, 70, 80 }; foreach (int s in scores) Console.WriteLine(s); ``` ### Lists and Dictionaries ```csharp using System.Collections.Generic; List<string> students = new List<string> { "Ava", "Ben", "Cara" }; students.Add("Dan"); Dictionary<string, int> grades = new(); grades["Ava"] = 95; ``` ### LINQ Basics ```csharp using System.Linq; var highScores = from g in grades where g.Value >= 90 orderby g.Key select g; foreach (var g in highScores) Console.WriteLine(quot;{g.Key}: {g.Value}"); ``` ### Mini-Project — Student Manager Implement add/remove/display for students with scores. ```csharp Dictionary<string, int> students = new(); while (true) { Console.Write("Command (add/list/quit): "); string cmd = Console.ReadLine(); if (cmd == "add") { Console.Write("Name: "); string name = Console.ReadLine(); Console.Write("Score: "); int score = int.Parse(Console.ReadLine()); students[name] = score; } else if (cmd == "list") { foreach (var s in students.OrderByDescending(s => s.Value)) Console.WriteLine(quot;{s.Key} - {s.Value}"); } else if (cmd == "quit") break; } ``` --- ### ✅ End of Phase 1 Summary By now you can: - Write, compile and debug C# programs. - Use variables, operators and control structures. - Handle collections and simple data queries. Next: **Phase 2 – Object Power (Lessons 6–9)** We’ll dive into Classes, OOP principles, and design patterns. --- Confirm when you’ve copied Phase 1 into your workspace and I’ll immediately generate **Phase 2 (Lessons 6–9)**.