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

A parenthesized pattern is a pattern matching construct in C# that encloses an existing pattern within parentheses `()`. Its primary function is to explicitly define the order of evaluation and enforce grouping within complex logical patterns, overriding the default precedence of the `and`, `or`, and `not` pattern combinators.

## Syntax

```csharp theme={"dark"}
( <pattern> )
```

The `<pattern>` can be any valid C# pattern, including relational patterns, type patterns, property patterns, or other logical patterns.

## Mechanics and Precedence

In C# pattern matching, logical combinators are evaluated based on a strict default precedence:

1. `not` (Highest)
2. `and`
3. `or` (Lowest)

When a compiler evaluates a compound pattern without parentheses, it binds `not` first, then groups `and` patterns, and finally evaluates `or` patterns. The parenthesized pattern forces the compiler to evaluate the enclosed pattern as a single, isolated unit before applying adjacent combinators.

### Altering `and` / `or` Precedence

Without parentheses, `and` binds tighter than `or`. Parentheses invert this relationship by grouping the `or` condition.

```csharp theme={"dark"}
// Default precedence: Evaluates as (type is int AND value > 10) OR (type is string)
bool defaultPrecedence = input is int and > 10 or string;

// Parenthesized precedence: Evaluates as type is int AND (value > 10 OR value < 0)
bool groupedPrecedence = input is int and (> 10 or < 0);
```

### Grouping with `not`

The `not` combinator only negates the immediately following pattern. To negate a compound logical pattern, the parenthesized pattern is required to encapsulate the entire expression.

```csharp theme={"dark"}
// Negates only the 'string' type pattern. 
// Evaluates as: (NOT string) OR int
bool unparenthesizedNot = input is not string or int;

// Negates the entire grouped pattern.
// Evaluates as: NOT (string OR int)
bool parenthesizedNot = input is not (string or int);
```

## Nesting

Parenthesized patterns can be nested arbitrarily deep to construct highly specific evaluation trees. The innermost parentheses are evaluated first.

```csharp theme={"dark"}
bool nestedPattern = input is (int and (> 0 and <= 100)) or (string and not ("" or null));
```

## Interaction with Declaration Patterns

When a parenthesized pattern contains a declaration pattern (which assigns a matched value to a variable), the compiler enforces strict rules regarding variable declarations across logical combinators.

Specifically, if a parenthesized declaration pattern is used as a branch within an `or` pattern, the exact same variable (matching both name and type) must be declared in *all* branches of that `or` pattern. Failure to declare the variable in every branch results in a hard compiler error, as the compiler dictates that any variable introduced in an `or` pattern must be guaranteed to be initialized regardless of which branch succeeds.

```csharp theme={"dark"}
// Valid: 'x' is declared in a standalone parenthesized pattern.
bool isMatch = input is (int x and > 0); 

// Compiler Error CS8784: Pattern variable 'y' must be declared in all cases of the 'or' pattern.
// This pattern is entirely illegal to write because 'y' is only declared in the left branch.
bool isInvalid = input is (int y and > 0) or string; 
```

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