C# Reference ยท Classes and Object-Oriented Concepts
Switch expressions
Reference for C# switch expressions: arrow arms, patterns, when guards, tuple switches, throw expressions, exhaustiveness and SwitchExpressionException.
A switch expression chooses one of several results based on the shape of a value. It is written value switch { pattern => result, ... } and, unlike the switch statement, it produces a value, so it can be assigned, returned, passed as an argument or used in an expression-bodied member. There is no case, no break and no fall-through.
Arms are tested from top to bottom and the first match wins. The compiler checks that the arms cover every possible input (exhaustiveness) and warns when they do not. If an unlisted value still arrives at run time, a SwitchExpressionException is thrown.
string size = count switch { 0 => "none", 1 => "one", _ => "many" };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
result = input switch { pattern1 => expression1, pattern2 when condition => expression2, _ => defaultExpression };
| Part | Description |
|---|---|
input |
Any expression. It is evaluated once. |
pattern |
Any pattern: constant, type, relational, property, positional, list, logical. |
when condition |
Optional boolean guard evaluated only if the pattern matched. |
expression |
Result of the arm. All arms must convert to a common type (the "natural type"), or to the target type when there is one. |
_ |
Discard; matches anything not matched earlier. |
Errors and warnings: a non-exhaustive switch gives a compile-time warning, and at run time throws SwitchExpressionException from System.Runtime.CompilerServices (its UnmatchedValue holds the value). A run of 5 switch { 1 => "one", 2 => "two" } printed warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '0' is not covered. and then Non-exhaustive switch expression failed to match its input. Unmatched value was 5.
Usage notes
- Every arm is an expression, not a statement. To run several statements, call a method or use a lambda. An arm may be a
throwexpression (second example). - Exhaustiveness for enums is not total. Because any integer can be cast to an enum, a switch that names every constant still warns (
CS8524, "involving an unnamed enum value"), andPrice((Plan)99)throws. Add_ => throw ...for defensive code. - Order matters. An arm that can never match because an earlier arm covers it is a compile-time error.
- Tuple switches match two or more values at once without nesting, which is ideal for state machines and decision tables.
- Arms are not evaluated in parallel. Guards run in order; avoid side effects in them.
- Type of the result.
x switch { 1 => 1, _ => 2.5 }has typedouble. If the arms have no common type, add a target type:double d = ...or cast one arm. - Different from the statement. A
switchstatement usescase ...:and may fall through. Seeswitch. - Compiles to efficient code. The compiler uses jump tables or binary search for constants, so a switch expression is as fast as, or faster than, an
ifchain. - Added in C# 8; relational and logical patterns in C# 9; list patterns in C# 11.