Perfect — here’s the **final phase** of your _C# Masterclass: From Zero to Hero_.
This is **PHASE 5 – THE DEVELOPER’S MIND (Lessons 16–19)** — focused on the professional skills and workflows that turn a programmer into a real engineer.
---
# 🧠 C# MASTERCLASS — PHASE 5 THE DEVELOPER’S MIND
**Goal:** Learn the habits, tools, and workflows of production-ready C# developers — version control, testing, optimisation, and deployment.
---
## Lesson 16 – Git and Version Control
### Objectives
- Understand Git fundamentals
- Use GitHub for source management
- Learn commit discipline and branching strategies
### Key Commands
```bash
# Initialise repository
git init
# Stage and commit changes
git add .
git commit -m "Initial commit"
# Create and switch branches
git branch feature/login
git checkout feature/login
# Merge back to main
git checkout main
git merge feature/login
# Push to GitHub
git remote add origin https://github.com/username/repo.git
git push -u origin main
```
### .gitignore for C#
```
bin/
obj/
.vscode/
*.user
*.suo
*.db
```
### Best Practices
1. Commit small, logical changes often.
2. Write clear commit messages (imperative mood).
3. Use pull requests for review.
4. Branch per feature or fix.
### Mini-Project — Portfolio Repository
Create a public GitHub repo named **CSharpMasterclass**.
Add your previous lesson projects, commit each phase separately, and write descriptive README.md files.
---
## Lesson 17 – Testing and Test-Driven Development (TDD)
### Objectives
- Write automated tests with xUnit
- Understand assertions and test fixtures
- Apply the TDD cycle: _Red → Green → Refactor_
### Setup
```bash
dotnet new xunit -o CalculatorTests
dotnet add CalculatorTests reference ../Calculator
dotnet test
```
### Example – Calculator Tests
```csharp
using Xunit;
public class Calculator
{
public int Add(int a, int b) => a + b;
}
public class CalculatorTests
{
[Fact]
public void Add_ReturnsCorrectSum()
{
var calc = new Calculator();
Assert.Equal(4, calc.Add(2, 2));
}
}
```
### Mocking Example (Moq)
```bash
dotnet add package Moq
```
```csharp
using Moq;
public interface INotifier { void Notify(string message); }
public class OrderProcessor
{
private readonly INotifier _notifier;
public OrderProcessor(INotifier notifier) => _notifier = notifier;
public void Process() => _notifier.Notify("Order complete");
}
[Fact]
public void Process_SendsNotification()
{
var mock = new Mock<INotifier>();
var processor = new OrderProcessor(mock.Object);
processor.Process();
mock.Verify(n => n.Notify("Order complete"), Times.Once());
}
```
### Mini-Project — Test Suite for Student Manager
Add xUnit tests that verify adding, removing, and sorting students.
Use TDD: write failing test → implement → verify.
---
## Lesson 18 – Performance Profiling and Optimisation
### Objectives
- Measure execution time and memory usage
- Identify bottlenecks
- Apply optimisation techniques safely
### Stopwatch Example
```csharp
using System.Diagnostics;
Stopwatch sw = Stopwatch.StartNew();
// Code block
sw.Stop();
Console.WriteLine(
quot;Elapsed: {sw.ElapsedMilliseconds} ms");
```
### BenchmarkDotNet Setup
```bash
dotnet new console -o Benchmarks
dotnet add package BenchmarkDotNet
```
Example:
```csharp
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
public class MathBenchmarks
{
[Benchmark]
public double LoopSum()
{
double total = 0;
for (int i = 0; i < 1000000; i++)
total += Math.Sqrt(i);
return total;
}
}
BenchmarkRunner.Run<MathBenchmarks>();
```
### Optimisation Tips
- Minimise allocations inside loops.
- Prefer `Span<T>` and `StringBuilder` for heavy text operations.
- Cache expensive results.
- Measure before and after each change.
### Mini-Project — Optimise the Web Scraper
Profile the Phase 4 scraper; use parallel batching, caching, and efficient I/O to reduce total runtime.
---
## Lesson 19 – Deployment and Documentation
### Objectives
- Package and publish .NET applications
- Create documentation and API help
- Automate builds and releases
### Publishing
```bash
dotnet publish -c Release -r win-x64 --self-contained true
```
This creates a standalone executable in `bin/Release/net8.0/win-x64/publish`.
### Configuration Files
Use `appsettings.json` for environment-specific settings.
```json
{
"Logging": { "Level": "Information" },
"ConnectionStrings": {
"Default": "Data Source=tasks.db"
}
}
```
### XML Documentation
```csharp
/// <summary>
/// Calculates total cost including tax.
/// </summary>
public double Total(double price, double taxRate) => price * (1 + taxRate);
```
Enable in `.csproj`:
```xml
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
```
### Continuous Integration (CI)
Use GitHub Actions:
```yaml
name: .NET Build
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
- run: dotnet build --configuration Release
```
### Mini-Project — Deploy Your Capstone
1. Package your final project (`dotnet publish`).
2. Create a GitHub release with binaries.
3. Include a README and generated docs folder.
4. Optionally deploy web version to Azure or Render.
---
## ✅ FINAL GRADUATION SUMMARY
You have completed **all 19 lessons** of the **C# Masterclass: From Zero to Hero**.
You can now:
- Build, debug, and optimise complex C# applications.
- Apply OOP and design patterns cleanly.
- Work with files, APIs, and databases.
- Implement async workflows and reflection.
- Test, profile, and deploy professional code.
**Suggested Next Steps**
1. Start contributing to open-source C# repositories.
2. Explore advanced frameworks — ASP.NET Core, MAUI, Unity.
3. Learn architectural patterns: CQRS, Domain-Driven Design.
4. Mentor others — teaching will solidify your mastery.
---
🎓 **Congratulations — you’re officially a C# Developer.**
Copy this entire Phase 5 into your workspace to complete your course materials.