> ## 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# Relational Pattern

A relational pattern in C# is a pattern matching construct introduced in C# 9.0 that evaluates whether an expression satisfies a relational operator condition against a constant value. It extends the `is` operator and `switch` statements/expressions by allowing direct magnitude comparisons without requiring explicit variable declarations or complex `if-else` chains.

## Syntax and Operators

A relational pattern consists of a relational operator followed by a constant expression. The supported operators are:

* `<` (less than)
* `<=` (less than or equal to)
* `>` (greater than)
* `>=` (greater than or equal to)

```csharp theme={"dark"}
int expression = 42;

// Syntax within an 'is' expression
bool match = expression is > 0;

// Syntax within a switch expression
int result = expression switch
{
    < 0 => -1,
    > 0 => 1,
    _ => 0
};
```

## Technical Constraints

1. **Constant Right-Hand Side:** The value on the right side of the relational operator must be a compile-time constant. You cannot use variables, method returns, or dynamically evaluated expressions as the target of the pattern.
2. **Supported Types:** Relational patterns are restricted to types that have built-in relational operators. This includes:
   * All integral and floating-point numeric types (`int`, `double`, `decimal`, `byte`, `nint`, etc.)
   * `char`
   * `enum` types
3. **Nullability:** If the input expression is a nullable value type (e.g., `int?`), a relational pattern implicitly checks for null. A null value will safely fail the pattern match without throwing a `NullReferenceException`.

## Code Mechanics

The following demonstrates the structural application of relational patterns across different C# constructs:

```csharp theme={"dark"}
int numericValue = 42;

// 1. Evaluation via 'is' operator
bool isPositive = numericValue is > 0;

// 2. Evaluation within a switch statement
switch (numericValue)
{
    case < 0:
        Console.WriteLine("Negative");
        break;
    case >= 0:
        Console.WriteLine("Non-negative");
        break;
}

// 3. Evaluation within a switch expression
string magnitude = numericValue switch
{
    < 10 => "Single digit",
    >= 10 => "Multiple digits"
};
```

## Composition with Logical Patterns

Relational patterns are strictly single-operator. To define bounded ranges, they must be composed using C# logical patterns (`and`, `or`, `not`). The compiler evaluates these composite patterns to ensure type safety and logical coherence.

```csharp theme={"dark"}
int target = 25;

// Defining an inclusive range using the 'and' logical pattern
bool inRange = target is >= 10 and <= 50;

// Defining an exclusive boundary using the 'or' logical pattern
bool outOfBounds = target is < 0 or > 100;
```

## Compiler Behavior: Subsumption and Exhaustiveness

When relational patterns are used within `switch` expressions, the C# compiler performs static analysis to verify two conditions:

1. **Exhaustiveness:** The compiler checks if all possible values of the input type are handled. If the relational patterns leave a gap (e.g., `< 0` and `> 0`, leaving `0` unhandled) and no discard pattern (`_`) is provided, the compiler issues a warning (`CS8509`).
2. **Subsumption (Unreachability):** The compiler evaluates the order of relational patterns. If a broader pattern precedes a narrower pattern that it entirely overlaps, the compiler flags the narrower pattern as unreachable code (`CS8510`).

```csharp theme={"dark"}
int value = 15;

string result = value switch
{
    < 20 => "Matched first",
    < 10 => "Unreachable", // Compiler Warning CS8510: The pattern is unreachable.
    _ => "Default"
};
```

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