C# pattern matching lets you test shape and values in a single expression — in is, switch statements and switch expressions. Pattern Example Type obj is string s Constant x is null, status is "active" Relational score is >= 90 Logical c is >= 'a' and <= 'z' or '', x is not null Property order is { Status: "Paid", Total: > 100 } Positional point is (0, 0) (records / Deconstruct) List (C# 11) args is [var first, .., var last] Discard => "default" Switch expressions are exhaustive-checked by the compiler and return a value, replacing long if/else chains.
Key Takeaways
Switch expressions return values and are checked for exhaustiveness.
Combine relational, logical, property and positional patterns.
List patterns ([first, .., last]) match arrays and lists.
Use when clauses for conditions that patterns cannot express.