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 is operator evaluates type compatibility and performs pattern matching at runtime. It returns a bool indicating whether the runtime type of an expression is compatible with a specified type or satisfies a specified pattern.

Type Testing

In its most fundamental form, the is operator checks if the runtime type of an expression is compatible with a given type.
expression is Type
The operation evaluates to true if the following conditions are met:
  • expression is not null.
  • expression can be cast to Type via a reference conversion, a boxing conversion, or an unboxing conversion.
The is operator does not consider user-defined conversions or numeric conversions. For example, an int evaluated against a double returns false, even though an implicit numeric conversion exists.

Pattern Matching Syntax

Starting with C# 7.0, the is operator was expanded to support pattern matching, allowing it to evaluate complex conditions, deconstruct types, and extract values simultaneously. Declaration Pattern Evaluates type compatibility and, upon a true result, assigns the casted instance to a newly declared local variable.
expression is Type variableName
var Pattern Matches any expression and assigns it to a newly declared local variable. Unlike type declaration patterns, the var pattern always evaluates to true and successfully matches even if the expression evaluates to null.
expression is var variableName
Constant Pattern Evaluates whether the expression equals a specified constant value. When evaluating null, this pattern bypasses any overloaded == or != operators, guaranteeing a safe reference check.
expression is null
expression is 42
Relational and Logical Patterns Evaluates the expression against relational operators (<, >, <=, >=) and combines multiple patterns using logical combinators (and, or, not). The right-hand operand of a relational pattern must be a constant expression; local variables are invalid C# semantics in this context.
expression is >= 0 and <= 100
expression is not Type
Property Pattern Evaluates whether the expression matches a specific type and recursively applies patterns to the properties or fields of that instance.
expression is Type { PropertyName: nestedPattern }
Positional (Deconstruction) Pattern Deconstructs an expression into its component parts and applies nested patterns to those parts. This requires the expression type to be a tuple or to possess a compatible Deconstruct method.
expression is (int x, int y)
expression is Type (nestedPattern1, nestedPattern2)
List Pattern Evaluates sequences (such as arrays, lists, or spans) to match elements against nested patterns. It supports the slice pattern (..) to match zero or more elements.
expression is [1, 2, ..]
expression is [var first, .., var last]

Evaluation Rules and Compilation Behavior

  • Null Handling: If expression evaluates to null, any type check (expression is Type) strictly returns false because there is no allocated instance to possess a type.
  • Static Typing Warnings: If the compiler determines at compile-time that the is operation will always return true or always return false based on the static types of the operands, it generates a compiler warning (CS0183 or CS0184) to indicate redundant code.
  • Variable Scope: Variables introduced via declaration, var, positional, or list patterns leak into the enclosing scope. They are definitively assigned only if the is expression evaluates to true.
Master C# with Deep Grasping Methodology!Learn More