Why this post exists
This is Part 2 of Day 4 in my six-day C# deep dive. Part 1 covered extension methods, pattern matching and records. This post covers the tools that make real programs reliable: cleaning up resources with IDisposable , JSON, unit testing, and dependency injection .
Each concept has what it is, an analogy, a short example, a practice question and a complete solution with the reasons it is written that way. Concepts 10 to 13 continue the numbering from Part 1. Try each practice question yourself before reading the solution.
All code targets .NET 8 or later. Most examples use top-level statements, so you can paste them into the Program.cs of a new console project. The testing section uses a small library project plus a test project, and each file is labelled.
Concept 10: IDisposable, using and resource cleanup
What it is. Memory is cleaned up automatically by the garbage collector, but some resources are not just memory: file handles, database connections, network sockets.…