C# Mastery

Advanced Pattern Matching (Switch Expressions)

1 Views Updated 5/4/2026

Advanced Pattern Matching

Pattern matching is the most powerful logic evolution in C# history. It allows you to test variables against "shapes" and extract data simultaneously. It has almost entirely replaced the old, bulky switch statements in modern enterprise codebases.

1. The Evolution: Switch Expressions

Instead of the verbose case: and break; syntax, we use a concise, functional-style arrow syntax.

int categoryId = 1;

string categoryName = categoryId switch 
{
    1 => "Electronics",
    2 => "Clothing",
    3 => "Books",
    _ => "Other" // The '_' is the default "discard" pattern
};

2. Relational & Logical Patterns

You can now perform mathematical and logical checks directly inside a switch statement!

var temp = 25;

string status = temp switch 
{
    < 0 => "Freezing",
    >= 0 and <= 20 => "Cold",
    > 20 and < 35 => "Warm",
    >= 35 => "Hot",
    _ => "Unknown"
};

3. Property Pattern Matching

You can "reach inside" an object to check its internal property values without writing nested if statements.

if (user is { IsAdmin: true, IsActive: true }) 
{
    Console.WriteLine("Access Granted!");
}

4. Interview Mastery

Q: "What is the 'is' keyword pattern, and how does it make our code safer during type-casting?"

Architect Answer: "The 'is' pattern combines two steps into one: Type Checking and Variable Assignment. In legacy C#, you had to check if an object was a string, and then cast it: `if (obj is string) { var s = (string)obj; ... }`. This is risky. With Pattern Matching, we write: `if (obj is string message)`. If the check succeeds, C# automatically creates the `message` variable, casts it, and makes it available inside the scope. This eliminates the 'Double-Cast' performance penalty and prevents `InvalidCastException` crashes entirely."

C# Mastery
1. Modern C# & Framework Fundamentals
Introduction to the .NET Ecosystem & Runtime (JIT, CLR) C# 12/13 Top-Level Statements & Global Usings Variables, Data Types, and Value vs Reference Deep Dive Mastering Nullable Reference Types & Null Safety The Magic of Strings (Interpolation, Verbatim, and Immutability)
2. Control Flow & Logical Structures
Advanced Pattern Matching (Switch Expressions) The Precision of Numbers: Checked vs Unchecked Math Iterators: Foreach, Yield Return, and Deferred Execution Defensive Programming: Guard Clauses and Exception Mastery
3. Object-Oriented Mastery
Classes vs Structs vs Records (Which to use when?) Mastering Primary Constructors & Object Initializers Interface Architectures: Default Implementations & Segregation Polymorphism vs Composition (The Architect's Dilemma) Partial Classes, Extension Methods, and Static Classes
4. Functional C# & Collections
Delegates, Func, Action, and Predicates Lambda Expressions & The Evolution of Linq Dynamic Array Management: List<T>, Dictionary, and HashSet Custom Collections & Yielding Data streams
5. Asynchronous & Parallel Programming
Async/Await Deep Dive: Task Lifecycle & Thread Safety Synchronization Context & Avoiding Deadlocks Parallel.ForEach vs PLINQ vs Tasks CancellationToken Mastery: Surgically Aborting Operations
6. Advanced Engineering & High Performance
Generics & Constraints: Building Type-Safe Libraries Reflection and Attributes: Reading Metadata at Runtime Dependency Injection Internals (ServiceCollection from scratch) High-Performance Memory: Span<T> and ReadOnlySpan<T> Garbage Collection Segments & LOH Internals Managing Unmanaged Resources: IDisposable & Finalizers Introduction to Source Generators & Interceptors C# Interview Masterclass: Architect-Level Explanations