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.

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

( <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.
// 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.
// 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.
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.
// 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; 
Master C# with Deep Grasping Methodology!Learn More