C# 8.0 added switch expressions and recursive property and positional patterns—not pattern matching itself. Use patterns to test a value’s type, constant value, or structure, then choose a branch or produce a result. A switch expression is a good fit when each matching case returns one value; a switch statement or if chain is often clearer for statement-oriented work.
What pattern matching does in C#
A pattern tests the value of an expression against a condition. Depending on the pattern, that condition can check the value’s runtime type, compare it with a constant, or inspect its structure. A successful type or declaration pattern can also make a variable available in the matching branch.
Patterns can appear in an is expression, a case label in a switch statement, or an arm of a switch expression. Microsoft’s pattern-matching overview describes these uses and the available pattern categories. C# 8.0 expanded the language with switch expressions and recursive patterns; it did not introduce the overall feature.
Use a switch expression to produce a value
A switch expression evaluates one input and selects a result from ordered arms. Each arm has a pattern, an optional when guard, an arrow, and a result expression. For example:
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errors#1 Best Overall
static string Describe(Delivery? delivery) => delivery switch
{
null => "No delivery",
ExpressDelivery express => $"Express: {express.TrackingCode}",
StandardDelivery { Days: <= 2 } => "Arriving soon",
_ => "Standard delivery"
};
The input expression, delivery, is evaluated for the switch. Arms are considered from top to bottom; the first arm whose pattern matches and whose guard (if present) succeeds supplies the result. Here, null is handled explicitly, the type pattern binds an ExpressDelivery value to express, and the property pattern checks a member. The discard pattern _ acts as a catch-all for anything not matched earlier.
Microsoft’s switch-expression reference describes the construct as a way to evaluate a single expression from candidate expressions. Choose it when each branch naturally returns a value, such as a string, number, or object.
Rank #2
Choose between an expression, a statement, and an if chain
- Switch expression: use it when a set of alternatives each produces one value and fits clearly into an expression.
- Switch statement: use it when cases need to perform multiple statements or other statement-oriented work.
ifchain: keep it when the condition is naturally a simple Boolean decision or the switch syntax would make the logic harder to read.
Pattern matching is a way to express a decision, not a reason to turn every conditional into a switch. The choice should make both the conditions and the resulting work easy to follow.
Inspect an object with a property pattern
A property pattern identifies parts of a value by member name. In StandardDelivery { Days: <= 2 }, the type is checked and the Days property is tested against a relational pattern. Property names make the condition visible, which is often helpful for classes, structs, and records.
Rank #3
Property patterns are recursive: a property can itself be matched with a pattern, allowing a condition to describe nested data. Microsoft explains the syntax and behavior in its property and positional patterns guide.
Match values by position with positional patterns
A positional pattern matches components in order rather than naming each component. Tuples have an established order. For other types, an accessible Deconstruct method defines the component values and their order.
Use positional matching when those positions already have obvious meaning—for example, a tuple created specifically to hold a small, well-understood set of values. If a reader has to look up what the second or third position represents, a property pattern is usually clearer. The positional pattern relies on that ordering contract, so changes to the order can change what the pattern means.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Handle nulls, arm order, and unmatched values
Account for null when using recursive patterns
Property and positional patterns do not match null. If the input can be null, add a null arm or check for a non-null value before applying a recursive pattern.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Put narrow cases before broad cases
Switch-expression arms are tested in source order. Place specific cases before a general catch-all: if an earlier pattern already covers every value a later arm could match, the later arm is subsumed and the compiler reports it as unreachable. A when guard adds a Boolean condition when the pattern alone does not express the full case. Keep guards understandable, especially when multiple arms could otherwise match the same input.
Decide what a missing match should mean
A switch expression needs a matching arm at runtime. If no arm matches, it throws: Microsoft documents SwitchExpressionException on .NET Core 3.0 and later, and InvalidOperationException on .NET Framework. Add a discard arm when the domain calls for a defined fallback. If unexpected inputs should instead be treated as errors, make that behavior explicit rather than silently mapping them to a default result.
Use C# 8.0 patterns without importing later syntax
The examples here use C# 8.0-era features, including switch expressions and recursive property and positional patterns. Microsoft’s current pattern documentation also describes syntax added in later C# versions. Do not assume that every pattern shown in current documentation is available in C# 8.0; list patterns, for example, are not a C# 8.0 feature.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Recommended Free Tools

