Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Operators and Expressions

C# pattern matching operators: is, not, and, or

Reference for C# pattern matching: the is operator, switch patterns, type, constant, relational, property, positional, list and logical patterns, var and discard.

A pattern describes the shape or value a piece of data must have. The is operator tests a value against a pattern and returns a bool; switch statements, switch expressions and case labels use the same patterns to pick a branch. A pattern can also declare variables that hold parts of the matched value, already converted to the right type.

C# has many pattern kinds, and most of them can be combined with the logical pattern keywords and, or and not and with parentheses. This page lists them all; the lessons teach them step by step.

C#
if (shape is Circle { Radius: > 10 } big)
{
    Console.WriteLine($"big circle {big.Radius}");
}

Try it

C# All the pattern kinds

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# is, is not and declaration patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
expression is pattern
expression is not pattern

expression switch { pattern [when condition] => result, ... }
case pattern [when condition]:
Pattern Example Matches
Declaration string s Value of that type; assigns s.
Type string Value of that type, no variable.
Constant 42, "x", null, Color.Red Value equal to the constant.
Relational > 5, <= 'z' Number or char in that relation.
Logical p1 and p2, p1 or p2, not p Combination of patterns.
Property { Length: > 3 }, { A.B: 1 } Non-null object whose members match.
Positional (0, var y), Point(1, 2) Deconstructed values match.
List / slice [1, .., var last], [_, _] Sequence with a given shape.
var var x Anything (including null); assigns x.
Discard _ Anything; no variable.
Parenthesized (p) Groups for precedence.

Returns: is yields bool. A switch expression yields the arm's value.

Errors: a switch expression with no matching arm throws SwitchExpressionException; the compiler warns when arms are not exhaustive and errors when an arm can never match because earlier arms cover it (subsumption).

Usage notes

  • Null. A type or declaration pattern never matches null; var and _ do. A property pattern fails on null, so x is { } means "not null".
  • Variables are definitely assigned only where the match is certain. After if (o is string s) you may use s inside the block; if (o is not string s) return; makes s available afterwards.
  • and binds tighter than or. is >= 0 and < 10 or 100 means (>= 0 and < 10) or 100. Use parentheses for clarity.
  • Order matters in switches: the first matching arm wins, and a more general pattern before a specific one is a compile error.
  • Constant patterns use ==-like semantics for numbers, and Equals for strings. Pattern matching on enums does not require qualifying every case beyond the type name.
  • Positional patterns need a Deconstruct method (records and tuples have one).
  • Prefer is null and is not null to == null: they ignore overloaded operators. Prefer is not to !(x is T).
  • switch statements use case ... when and break; expressions use => arms and need to be exhaustive.
  • Performance: the compiler turns patterns into type checks, comparisons and jump tables; there is no reflection.