QuickDevGuide

Home › C#

Pattern Matching — C#

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