Chapter 23: Modern C# Features (C# 9–12+)

Today we’re on Chapter 23: Modern C# Features (C# 9–12+) – this is the chapter where we finally get to see how beautiful, clean, and expressive modern C# has become!

Starting from C# 9 (2020), Microsoft added a ton of features that make writing code much shorter, safer, more readable, and more fun. Many of these features are now considered best practice in 2026, and most new projects use them by default.

We’re going to cover the most important modern features you’ll see and use every day:

  • Records (immutable data classes)
  • Init-only properties (set only at creation)
  • Top-level statements (no Main method!)
  • Pattern matching enhancements (super powerful!)
  • Source generators (magic code generation)

I’m going to explain everything very slowly, step by step, with tons of real-life examples, before & after comparisons, and practical mini-projects — just like we’re sitting together in Hyderabad looking at the same screen. Let’s dive in! 🚀

1. Records – Immutable Data Classes (C# 9+)

Records are the modern way to create simple data classes (like DTOs, value objects, data transfer objects).

Why records are awesome:

  • Immutable by default (great for thread safety & functional programming)
  • Value equality (two records are equal if all properties are equal – not just reference!)
  • With-expressions (create a copy with one field changed – super clean)
  • ToString(), Equals(), GetHashCode() are automatically generated
  • Much shorter syntax

Old way (C# 8 or earlier):

C#

Modern way with record:

C#

Usage – value equality & with-expression:

C#

Positional records vs nominal records:

C#

2. Init-only Properties – Set Only at Creation (C# 9+)

Init-only properties can be set only during object initialization – perfect for immutable objects.

Old way (read-only):

C#

Modern way with init:

C#

Records automatically make primary constructor parameters init-only!

3. Top-level Statements – No More Boilerplate (C# 9+)

Old way (classic Program.cs):

C#

Modern way (C# 9+ – top-level statements):

C#

Even better (C# 10+): You can have implicit usings and global usings – no need to write using System; everywhere!

File: GlobalUsings.cs (create this file)

C#

Now your Program.cs becomes super clean!

4. Pattern Matching Enhancements – Super Powerful (C# 9–12+)

Pattern matching lets you check types and values in a very expressive way.

Old way:

C#

Modern patterns (C# 9–12):

C#

5. Source Generators – Magic Code Generation (C# 9+)

Source generators are like Roslyn-based macros – they automatically generate code at compile time.

Most famous examples:

  • System.Text.Json source generator (fast JSON serialization)
  • Microsoft.Extensions.Options validation
  • AutoMapper source generator
  • Mediator pattern generators

Real example (simple one):

Many libraries use source generators so you don’t write boilerplate.

Example – JSON source generator (recommended in 2026):

C#

Mini-Project: Modern Person Manager with Records & Top-Level Statements

Program.cs (modern style – no class, no Main!)

C#

Summary – What We Learned Today

  • Records → short, immutable, value-equality data classes
  • Init-only properties → set only during object creation
  • Top-level statements → clean, minimal Program.cs
  • Pattern matching → is, switch, relational, logical, list patterns
  • Source generators → automatic code generation at compile time (fast JSON, etc.)

Your Homework (Super Practical!)

  1. Create a new console project called ModernCSharpMaster (use .NET 10!)
  2. Use records to create:
    • Product (Name, Price, Category)
    • Order (Id, CustomerName, List<Product> Items)
  3. Use top-level statements in Program.cs
  4. Use pattern matching to:
    • Check if order total > ₹5000 (discount message)
    • Classify products by price range
  5. Serialize the order to JSON and save to file
  6. Bonus: Use with-expression to create a discounted order

Next lesson: Best Practices & Design Patterns – we’re going to learn how to write professional, clean, maintainable C# code!

You’re doing absolutely fantastic! 🎉 Any modern feature confusing? Want more examples with records, patterns, or source generators? Just tell me — I’m right here for you! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *