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

A switch expression is a pattern-matching construct introduced in C# 8.0 that evaluates a target object against a series of patterns and returns a single value based on the first matching pattern. Unlike the traditional `switch` statement, which executes blocks of code via control flow, a switch expression is an expression that yields a result, enabling more concise syntax and functional programming paradigms.

## Core Syntax

The syntax reverses the traditional order by placing the target expression before the `switch` keyword, followed by a comma-separated list of expression arms.

```csharp theme={"dark"}
return_type result = target_expression switch
{
    pattern_1 => expression_1,
    pattern_2 => expression_2,
    _ => default_expression
};
```

## Technical Mechanics

* **Target Expression:** The variable or value being evaluated.
* **Expression Arms:** Each arm consists of a pattern, a fat arrow (`=>`), and the expression to return if the pattern matches. Arms are separated by commas, not semicolons.
* **Discard Pattern (`_`):** Functions as the default catch-all. It matches any value that has not been matched by preceding patterns.
* **Exhaustiveness Checking:** The C# compiler performs static analysis to ensure all possible values of the target expression's type are handled. If the compiler detects missing patterns, it generates a warning (CS8509).
* **Runtime Behavior:** If a switch expression evaluates a value that does not match any pattern and no discard pattern is provided, the runtime throws a `System.Runtime.CompilerServices.SwitchExpressionException`.

## Supported Pattern Types

Switch expressions leverage the full C# pattern-matching engine, allowing for complex evaluations within a single arm.

**1. Constant and Relational Patterns**
Evaluates exact values or compares values using relational operators (`<`, `>`, `<=`, `>=`).

```csharp theme={"dark"}
int status = code switch
{
    200 => 1,
    >= 400 and < 500 => -1, // Relational and logical patterns combined
    _ => 0
};
```

**2. Type and Declaration Patterns**
Checks the runtime type of the target expression and optionally binds it to a new local variable.

```csharp theme={"dark"}
string typeInfo = shape switch
{
    Circle c => $"Circle with radius {c.Radius}",
    Rectangle r => $"Rectangle {r.Length}x{r.Width}",
    null => "Null reference",
    _ => "Unknown shape"
};
```

**3. Property Patterns**
Matches against the properties or fields of the target object.

```csharp theme={"dark"}
decimal discount = customer switch
{
    { IsPremium: true, YearsActive: >= 5 } => 0.20m,
    { IsPremium: true } => 0.10m,
    _ => 0.0m
};
```

**4. Positional (Tuple) Patterns**
Evaluates multiple values simultaneously by deconstructing them into a tuple.

```csharp theme={"dark"}
string quadrant = (x, y) switch
{
    (0, 0) => "Origin",
    (> 0, > 0) => "Q1",
    (< 0, > 0) => "Q2",
    (_, 0) => "X-Axis", // Discard used for a specific tuple element
    _ => "Other"
};
```

## Case Guards

When a pattern alone is insufficient to express the required logic, a `when` clause (case guard) can be appended to an expression arm. The arm will only match if both the pattern matches and the boolean expression in the `when` clause evaluates to `true`.

```csharp theme={"dark"}
string evaluation = (x, y) switch
{
    (var a, var b) when a == b => "Symmetrical",
    (var a, var b) when a > b => "X-Dominant",
    _ => "Y-Dominant"
};
```

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