> ## 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.

# C# Switch Statement

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.

```csharp theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
