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.

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