Skip to main content
The property pattern is a pattern matching construct in C# that enables you to match an expression’s properties or fields against nested patterns. It evaluates to true if the input expression is non-null and every specified property or field matches its corresponding pattern.

Syntax

The property pattern is defined using curly braces { } containing a comma-separated list of property names, a colon :, and the nested pattern to match against that property.
It is most frequently combined with a type pattern to ensure the object is of a specific type before evaluating its properties:

Core Mechanics

Implicit Null Checking A property pattern inherently guarantees that the input is not null. If the input expression evaluates to null, the pattern immediately returns false without throwing a NullReferenceException. Consequently, an empty property pattern is commonly used as a concise non-null check.
Constant and Relational Matching Properties can be matched against constant values or evaluated using relational patterns (<, >, <=, >=).
Variable Capture (Declaration Pattern) You can extract property values directly into local variables during the match by combining the property pattern with a declaration pattern.
Nested Property Patterns Because the right side of the colon accepts any valid C# pattern, property patterns can be nested recursively to evaluate complex object graphs.
Extended Property Patterns (C# 10+) Starting in C# 10, you can reference nested properties directly using dot notation, eliminating the need for nested curly braces. This provides a more concise syntax for deep property evaluation.
Logical Combinators Property patterns fully support logical combinators (and, or, not) within the nested pattern evaluation.
Tired of Poor C# Skills? Fix That With Deep Grasping!Learn More