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

The `var` pattern is an irrefutable pattern matching construct in C# that matches any expression, including `null`, and assigns the evaluated result to a newly declared local variable. It relies on compiler type inference to determine the variable's type based strictly on the compile-time (static) type of the matched expression, without performing any runtime type checking.

```csharp theme={"dark"}
// Syntax
expression is var variableName
```

## Mechanical Characteristics

**1. Irrefutability**
Unlike type patterns or constant patterns, the `var` pattern is irrefutable. It always evaluates to `true`. Because it cannot fail, it is guaranteed to execute the associated branch when used in conditional constructs like `if` statements or `switch` expressions.

**2. Compile-Time Type Inference**
The type of the variable introduced by the `var` pattern is identical to the static type of the expression being matched. It does not unbox values or downcast to derived runtime types.

```csharp theme={"dark"}
object myData = "Hello World";

// 'v' is typed as System.Object at compile-time, not System.String.
// The match evaluates to true.
if (myData is var v) 
{
    // v.Length is invalid here because 'v' is an object.
}
```

**3. Null Inclusivity**
The `var` pattern successfully matches `null` values. This is a primary mechanical distinction between the `var` pattern and a type pattern using the base `object` type.

```csharp theme={"dark"}
string text = null;

// Evaluates to FALSE. Type patterns fail on null.
bool typeMatch = text is string s; 

// Evaluates to TRUE. Var patterns succeed on null.
bool varMatch = text is var v;     
```

## Variable Scoping and Definite Assignment

Variables declared via the `var` pattern follow standard pattern variable scoping rules. The variable is always introduced into the enclosing scope of the pattern expression, regardless of whether the pattern matches. However, whether the variable can be read depends on the compiler's definite assignment analysis.

Because the `var` pattern is irrefutable, it always succeeds, meaning the variable is unconditionally assigned and can be read in the enclosing block.

```csharp theme={"dark"}
if (CalculateValue() is var result)
{
    // 'result' is in scope and definitively assigned.
    Console.WriteLine(result);
}

// 'result' remains in scope in the enclosing block.
// Because the 'var' pattern is irrefutable, the 'if' condition is always true,
// meaning 'result' is definitively assigned and can be safely read here as well.
Console.WriteLine(result); 
```

## Interaction with Pattern Combinators

When used within complex pattern matching expressions, the `var` pattern captures the value of the expression at the specific point of evaluation. It is frequently used inside property patterns to capture intermediate values.

```csharp theme={"dark"}
// Captures the 'X' property into 'xVal' while simultaneously 
// checking if 'Y' is greater than 0.
if (point is { X: var xVal, Y: > 0 })
{
    // xVal is available here
}
```

However, C# enforces strict rules regarding variable declarations within logical pattern combinators (`and`, `or`, `not`):

* **`not` patterns:** It is strictly forbidden to declare a pattern variable under a `not` pattern. Attempting to do so results in Compiler Error CS8773.
* **`or` patterns:** A pattern variable can only be declared inside an `or` pattern if the exact same variable name and type are declared in *all* branches of the `or` pattern. This ensures the variable is definitively assigned regardless of which branch succeeds.
* **`and` patterns:** Variables can be freely declared in `and` patterns, provided they do not conflict with variables declared in other branches of the same pattern.

```csharp theme={"dark"}
// INVALID: Compiler Error CS8773. Cannot declare a variable under a 'not' pattern.
if (point is not var p) { }

// INVALID: 'zVal' is not declared in both branches of the 'or' pattern.
if (point is { X: > 0, Z: var zVal } or { Y: > 0 }) { }

// VALID: 'val' is declared in all branches of the 'or' pattern.
if (point is { X: var val } or { Y: var val }) { }
```

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