Excellent — continuing your **C# Masterclass**, here is **PHASE 2 – OBJECT POWER (Lessons 6–9)**. Paste these directly into Visual Studio Code or Obsidian as Markdown lessons. All examples are runnable with the .NET 8 SDK. --- # 🧠 C# MASTERCLASS — PHASE 2 OBJECT POWER **Goal:** Understand Object-Oriented Programming (OOP), apply inheritance and interfaces, learn design patterns, and write clean, maintainable code. --- ## Lesson 6 – The Art of Objects ### Objectives - Define classes and objects - Understand fields, properties, and methods - Use constructors and encapsulation ### Example – Simple Class ```csharp class Player { public string Name { get; set; } public int Health { get; private set; } public Player(string name) { Name = name; Health = 100; } public void TakeDamage(int amount) { Health -= amount; if (Health < 0) Health = 0; } } ``` ### Usage ```csharp var hero = new Player("Arden"); hero.TakeDamage(30); Console.WriteLine(quot;{hero.Name} has {hero.Health} HP"); ``` ### Concepts - **Encapsulation:** hide internal state with `private` fields - **Properties:** provide controlled access - **Constructors:** initialise objects ### Mini-Project — RPG Character Classes Create `Warrior`, `Mage`, and `Rogue` classes with unique methods. Write a test program that instantiates each and calls their abilities. --- ## Lesson 7 – Inheritance and Interfaces ### Objectives - Apply inheritance to share code - Override virtual methods - Use interfaces for contracts ### Example – Base and Derived ```csharp class Vehicle { public virtual void Drive() => Console.WriteLine("Vehicle drives."); } class Car : Vehicle { public override void Drive() => Console.WriteLine("Car drives on road."); } ``` ### Interfaces ```csharp interface IFlyable { void Fly(); } class Airplane : Vehicle, IFlyable { public void Fly() => Console.WriteLine("Airplane soars."); } ``` ### Mini-Project — Vehicle Factory Create `Vehicle`, `Car`, `Truck`, `Bike`, and `IFlyable`. Store them in a `List<Vehicle>` and loop calling `Drive()` or `Fly()`. --- ## Lesson 8 – Design Patterns that Think ### Objectives - Understand the **SOLID** principles - Learn common design patterns - Apply Factory, Strategy, and Observer ### Example – Factory Pattern ```csharp abstract class Enemy { public abstract void Attack(); } class Orc : Enemy { public override void Attack() => Console.WriteLine("Orc swings club"); } class Goblin : Enemy { public override void Attack() => Console.WriteLine("Goblin throws dagger"); } class EnemyFactory { public static Enemy Create(string type) => type switch { "orc" => new Orc(), "goblin" => new Goblin(), _ => throw new ArgumentException("Unknown type") }; } ``` ### Example – Strategy Pattern ```csharp interface IAttackStrategy { void Attack(); } class SwordAttack : IAttackStrategy { public void Attack() => Console.WriteLine("Slashes with sword!"); } class Archer { private IAttackStrategy _strategy; public Archer(IAttackStrategy strategy) => _strategy = strategy; public void PerformAttack() => _strategy.Attack(); } ``` ### Mini-Project — Stock Market Notifier (Observer) Implement a `Stock` class with `PriceChanged` event and observer classes that print updates. --- ## Lesson 9 – Clean Code & Dependency Injection ### Objectives - Apply clean code practices - Understand dependency injection (DI) - Build modular, testable components ### Clean Code Guidelines 1. Use clear names (`GetCustomerById`, not `DoThing`). 2. One purpose per method. 3. Avoid magic numbers. 4. Comment **why**, not **what**. 5. Consistent indentation and casing. ### Example – Tight vs Loose Coupling ```csharp // Tight coupling class EmailService { public void Send(string msg) => Console.WriteLine(quot;Email: {msg}"); } class OrderProcessor { private EmailService email = new EmailService(); public void Process() => email.Send("Order complete"); } ``` Refactor with DI: ```csharp interface INotifier { void Notify(string message); } class EmailNotifier : INotifier { public void Notify(string message) => Console.WriteLine(quot;Email: {message}"); } class OrderProcessor { private readonly INotifier _notifier; public OrderProcessor(INotifier notifier) => _notifier = notifier; public void Process() { Console.WriteLine("Order processed"); _notifier.Notify("Order complete"); } } // Usage var processor = new OrderProcessor(new EmailNotifier()); processor.Process(); ``` ### Mini-Project — Plugin-Based Calculator - Create an `IOperation` interface with `double Execute(double a, double b)` - Implement plugins (Add, Subtract, Multiply, Divide) - Use DI to inject operations into a `Calculator` class. --- ### ✅ End of Phase 2 Summary You now understand: - How to design and instantiate objects. - Inheritance, interfaces, and polymorphism. - Applying design patterns and clean architecture. Next up is **Phase 3 – The Real World (Lessons 10–12)**: working with files, APIs, databases, and simple UI/web apps. Confirm you’re ready and I’ll generate Phase 3 straight away.