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

A logical pattern in C# is a pattern matching construct that combines or negates other patterns using the `and`, `or`, and `not` pattern combinators. Introduced in C# 9.0, it allows the evaluation of multiple pattern matching conditions within a single expression, operating directly on patterns rather than evaluating standard boolean expressions. Logical patterns are exclusively utilized within pattern matching contexts, such as the `is` operator, `switch` statements, and `switch` expressions.

## Pattern Combinators

Logical patterns utilize three specific contextual keywords. They are distinct from standard boolean logical operators (`&&`, `||`, `!`) because they compose *patterns* rather than evaluating boolean variables.

### 1. The `not` Pattern (Negation)

The `not` pattern matches an expression if the negated pattern does *not* match the expression. It is frequently applied to constant patterns and type patterns.

```csharp theme={"dark"}
object variable = "Test";

// Syntax: expression is not <pattern>
bool result = variable is not null;
bool isNotString = variable is not string;
```

### 2. The `and` Pattern (Conjunction)

The `and` pattern matches an expression if *both* of the combined patterns match the expression. It is commonly used to combine relational patterns to define boundaries.

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

// Syntax: expression is <pattern1> and <pattern2>
bool result = numericValue is >= 10 and <= 20;
```

### 3. The `or` Pattern (Disjunction)

The `or` pattern matches an expression if *either* of the combined patterns matches the expression. It is typically used to match against multiple discrete constant patterns or distinct type patterns.

```csharp theme={"dark"}
int numericValue = 0;
object variable = "Test";

// Syntax: expression is <pattern1> or <pattern2>
bool result = numericValue is 0 or 100;
bool isTextOrValue = variable is string or ValueType;
```

## Operator Precedence and Grouping

When combining multiple logical patterns, the C# compiler enforces a strict order of precedence. From highest to lowest precedence, the order is:

1. `not`
2. `and`
3. `or`

To override the default precedence or to improve code readability, parentheses `()` are used to explicitly group patterns.

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

// Evaluates as: (variable is >= 10 and <= 20) or (variable is 100)
bool defaultPrecedence = variable is >= 10 and <= 20 or 100;

// Overriding precedence: variable must be greater than 0 AND (either 10 or 20)
bool explicitGrouping = variable is > 0 and (10 or 20);
```

## Composition with Other Patterns

Logical patterns act as a structural glue for other pattern types. They can compose:

* **Constant patterns:** `is 1 or 2`
* **Relational patterns:** `is > 0 and < 10`
* **Type patterns:** `is int or double`
* **Property patterns:** `is { Length: > 0 } and not { Length: 5 }`
* **Declaration patterns:** `is int x and > 0` (Note: C# strictly prohibits declaring variables inside `or` and `not` patterns. Attempting to do so results in compiler error CS8780).

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