Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The switch statement and expression

Reference for C# switch: statements with break, switch expressions, patterns, when guards, exhaustiveness and the SwitchExpressionException.

switch selects one of several branches according to the value of an expression. C# has two forms. The switch statement uses case labels, runs statements and needs an explicit break (or other jump) at the end of every section; there is no implicit fall-through. The switch expression uses => arms, produces a value and is the preferred style for mapping one value to another.

Both forms support patterns: constants, relational (> 10), type, property, positional, list patterns, and/or/not, and when guards. A switch expression must be exhaustive; if no arm matches at run time it throws SwitchExpressionException.

C#
string kind = day switch
{
    6 or 7 => "weekend",
    >= 1 and <= 5 => "weekday",
    _ => "invalid",
};

Try it

C# Switch expression

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.

C# Statement, shared sections and type patterns

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

C#
switch (expression)
{
    case pattern [when condition]:
        statements
        break;
    default:
        statements
        break;
}

result = expression switch
{
    pattern [when condition] => value,
    _ => value,
};
Name Description
expression The value to test. Any type, because patterns can match types and properties.
pattern A constant, relational, type, property, positional, list, var or discard pattern, combined with and, or, not.
when An optional boolean guard evaluated only after the pattern matched.
_ / default Matches anything not matched earlier.

Returns: the switch expression yields the value of the first matching arm; all arms must convert to a common type (or the expression is target-typed).

Errors: a section that can fall through gives CS0163. An arm that an earlier arm already covers is a compile-time error (CS8510). A non-exhaustive switch expression produces warning CS8509 and throws SwitchExpressionException at run time. Duplicate constant labels give CS0152.

Usage notes

  • No fall-through. Every section must end in break, return, throw, continue or goto case. Stacked labels with no statements in between share a section.
  • Order matters. Arms are tested from top to bottom, and the compiler rejects an arm that can never be reached.
  • Exhaustiveness. For enums the compiler wants every member plus a _ arm, because an enum variable can hold values outside the named set.
  • Switch expression arms are expressions, not statements; use throw expressions for impossible cases: _ => throw new ArgumentOutOfRangeException(nameof(n)).
  • Variable scope. A pattern variable declared in a case label is scoped to that section; declarations in a section's statements are shared across the whole switch block.
  • Performance. The compiler turns dense integer and string switches into jump tables or hash lookups, so a large switch is usually faster than a long if chain.
  • Language versions. Switch expressions arrived in C# 8, relational and logical patterns in C# 9, list patterns in C# 11.