Pattern Matching
A C# language feature that lets you test a value against a pattern and extract information from it in a single expression. C# pattern matching has grown significantly across recent versions: type patterns, property patterns, relational patterns, logical patterns (and, or, not). Combined with switch expressions, it produces concise, readable code for complex branching logic that would otherwise require rivers of if-else statements.
Example from Rune & Rust: Game state transitions are a natural fit:
var result = (currentState, action) switch
{
(GameState.Exploring, Action.Fight) => TransitionTo(GameState.Combat),
(GameState.Combat, Action.Flee) => TransitionTo(GameState.Exploring),
(GameState.Combat, Action.Win) => HandleVictory(),
(GameState.Inventory, Action.Use) => ApplyItem(),
_ => InvalidAction()
};
Five possible states, five possible outcomes, zero ambiguity. Try doing that with nested if statements without weeping gently.
Why it matters for writers: Pattern matching shows up across all the projects on this site. LlmsTxtKit uses it for parsing line types. Rune & Rust uses it for state machines. Lexichord uses it for routing content to the right AI capability. It's one of the C# features that makes code self-documenting, which, for a docs-first developer, is kind of the point.
Related terms: Records · Nullable Reference Types · File-Scoped Namespaces