Perfect — continuing your **C# Masterclass**, here is **PHASE 3 – THE REAL WORLD (Lessons 10–12)**.
These lessons move from theory to real-world, production-ready C#.
They include file handling, APIs, databases, and GUI / web development foundations.
---
# 🧠 C# MASTERCLASS — PHASE 3 REAL WORLD
**Goal:** Apply C# to real applications that communicate with files, APIs, and databases, then build simple interfaces.
---
## Lesson 10 – Files, JSON, and APIs
### Objectives
- Read and write text and binary files
- Parse and create JSON using `System.Text.Json`
- Consume REST APIs asynchronously
### File I/O Example
```csharp
using System.IO;
string path = "notes.txt";
File.WriteAllText(path, "Learning C# is powerful!");
string content = File.ReadAllText(path);
Console.WriteLine(content);
```
### JSON Example
```csharp
using System.Text.Json;
var person = new { Name = "Ava", Age = 30 };
string json = JsonSerializer.Serialize(person);
Console.WriteLine(json);
var copy = JsonSerializer.Deserialize<Dictionary<string, object>>(json);
Console.WriteLine(copy["Name"]);
```
### REST API Example
```csharp
using System.Net.Http;
using System.Threading.Tasks;
HttpClient client = new HttpClient();
string response = await client.GetStringAsync("https://api.github.com/zen",
new HttpRequestMessage().Headers.Add("User-Agent", "CSharpMasterclass"));
Console.WriteLine(response);
```
### Mini-Project — Weather Dashboard
- Ask user for a city name.
- Use OpenWeatherMap API to fetch current temperature.
- Display result in console.
_(Requires a free API key and `await client.GetStringAsync()`.)_
---
## Lesson 11 – Databases and Entity Framework Core
### Objectives
- Understand ORM basics
- Create models, contexts, and migrations
- Query and update data using LINQ
### Setup
Install EF Core:
```bash
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet add package Microsoft.EntityFrameworkCore.Tools
```
### Example – Model and Context
```csharp
using Microsoft.EntityFrameworkCore;
public class TaskItem
{
public int Id { get; set; }
public string Title { get; set; }
public bool IsDone { get; set; }
}
public class TaskContext : DbContext
{
public DbSet<TaskItem> Tasks { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options) =>
options.UseSqlite("Data Source=tasks.db");
}
```
### CRUD Operations
```csharp
using var db = new TaskContext();
db.Database.EnsureCreated();
db.Tasks.Add(new TaskItem { Title = "Learn EF Core" });
db.SaveChanges();
var tasks = db.Tasks.Where(t => !t.IsDone);
foreach (var t in tasks)
Console.WriteLine(t.Title);
```
### Mini-Project — Task Tracker DB
- Create CRUD console app using EF Core SQLite
- Features: add, list, mark done, delete
- Display all incomplete tasks each launch
---
## Lesson 12 – GUI and Web Applications
### Objectives
- Understand .NET UI frameworks
- Build a minimal web app with ASP.NET Core
- Build a basic desktop app with Windows Forms or WPF
### Console to Web Transition
#### ASP.NET Minimal API
```csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Welcome to the Lucky Griffon Dashboard!");
app.Run();
```
Run with:
```bash
dotnet run
```
Visit `http://localhost:5000`.
### Simple Razor Page Example
Create with:
```bash
dotnet new webapp -o MyWebApp
```
This scaffolds a project with MVC pattern.
### Mini-Project — To-Do Web App
- Create ASP.NET Core project
- Add Entity Framework model `TaskItem`
- Scaffold pages to add, edit, and list tasks
_(Use SQLite for local persistence.)_
---
### ✅ End of Phase 3 Summary
You can now:
- Read and write to the file system.
- Communicate with APIs.
- Persist data using EF Core.
- Serve and build simple web applications.
Next is **Phase 4 – Genius Level (Lessons 13–15)**:
asynchronous programming, reflection, generics, and the capstone project.
Would you like me to continue and generate **Phase 4 now**?