Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The switch statement is a selection control flow construct that evaluates a single match expression and transfers execution to the switch section associated with the first matching pattern. It provides a structured, highly optimized mechanism for multi-way branching based on constant evaluation or complex pattern matching.
int matchExpression = 42;
bool condition = true;

switch (matchExpression)
{
    case 0:
        // Executed if matchExpression equals the constant 0
        break;
        
    case int typeVar when condition:
        // Executed if matchExpression matches the type AND the guard condition is true
        break;
        
    case > 100:
        // Executed based on a relational operator
        break;

    case 1:
    case 2:
        // Stacked cases: Executed if matchExpression equals 1 OR 2
        break;

    default:
        // Executed if no preceding case patterns match
        while (true) { } // Satisfies the unreachable end point rule without a jump statement
}

Core Mechanics and Rules

1. The Match Expression The expression evaluated inside the switch parentheses can be of any non-pointer type. The compiler evaluates this expression exactly once and compares the result against the defined case labels sequentially (or via a jump table for constants). 2. Switch Sections and Labels A switch block contains one or more switch sections. Each section begins with one or more case labels (or the default label) and is followed by a list of statements. 3. No Implicit Fall-Through C# strictly prohibits implicit fall-through between non-empty switch sections. You cannot execute one case block and silently drop into the next. The C# language specification dictates that the end point of a switch section must not be reachable. This requirement is typically satisfied using explicit jump statements:
  • break: Exits the switch block entirely.
  • return: Exits the enclosing method.
  • throw: Throws an exception.
  • goto case [label] or goto default: Explicitly transfers control to another switch section.
However, the unreachable end point rule can also be satisfied without these specific jump statements, such as by using a continue statement (if the switch is nested within a loop) or by entering an infinite loop (e.g., while(true) { }). 4. Empty Case Stacking While implicit fall-through of statements is forbidden, you can stack multiple empty case labels together. If the match expression satisfies any of the stacked labels, the single shared statement block is executed. 5. The default Section The default label acts as the fallback execution path. It is optional and can appear anywhere within the switch block, though it is conventionally placed at the end. The compiler always evaluates the default case last, regardless of its physical placement.

Pattern Matching Capabilities

Modern C# extends the switch statement beyond simple constant equality, allowing it to evaluate complex patterns:
  • Constant Pattern: Tests if the expression equals a specified constant (e.g., case null:, case 42:).
  • Declaration/Type Pattern: Tests if the expression is of a specific type and, if successful, casts and assigns it to a new local variable (e.g., case string text:).
  • Relational Pattern: Compares the expression using relational operators (e.g., case > 0:).
  • Logical Pattern: Combines multiple patterns using and, or, and not keywords (e.g., case >= 1 and <= 10:).
  • Property Pattern: Matches properties or fields of an object (e.g., case { Length: > 0 }:).

Guard Clauses (when)

Any case label can be appended with a when keyword followed by a boolean expression. This is known as a guard clause. The switch section is only selected if both the pattern matches and the guard clause evaluates to true. If the guard clause evaluates to false, the switch statement continues evaluating subsequent case labels.
Master C# with Deep Grasping Methodology!Learn More