C# Reference ยท Classes and Object-Oriented Concepts
Property and positional patterns
Reference for C# property patterns { Name: pattern }, extended property patterns, positional patterns with Deconstruct, tuple patterns and null handling.
A property pattern tests members of an object: person is { Age: >= 18, Country: "NO" } is true when person is not null, its Age is at least 18 and its Country equals "NO". Each member gets its own nested pattern, so you can use constants, relational patterns, further property patterns, or var to capture the value. Fields work as well as properties.
A positional pattern matches by position instead of by name. It works for tuples and for any type that has a Deconstruct method (including positional records): point is (0, var y). Both kinds can be combined with a type and a designation: Circle { Radius: > 10 } big.
if (order is { Customer.Address.City: "Oslo", Total: > 100m }) { Console.WriteLine("big order in Oslo"); }
Try it
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.
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
expr is { Member: pattern, Other: pattern } expr is { A.B.C: pattern } // extended property pattern (C# 10) expr is Type { Member: pattern } name expr is (pattern1, pattern2) // positional expr is Type(pattern1, pattern2) name
| Part | Description |
|---|---|
{ } |
Matches any non-null value. A common spelling of "is not null". |
Member: pattern |
Reads a property or field and applies the nested pattern to its value. Several members are separated by commas (all must match). |
A.B.C: pattern |
Shorthand for { A: { B: { C: pattern } } }. |
(p1, p2, ...) |
Calls Deconstruct (or reads tuple items) and matches each element with the corresponding pattern. |
Type and name |
Optional type test and variable to hold the matched value. |
Usage notes
- A property pattern implies a null check.
{ }and{ Age: 18 }never matchnull, so you can often drop explicit null tests. - Nested null.
{ Address.City: "Oslo" }fails (does not throw) whenAddressisnull, because the extended form includes the null test onAddress. - Properties are called, not cached. A getter may be evaluated more than once, so avoid expensive or side-effecting properties in patterns.
- Positional patterns need
Deconstruct. Records and tuples have it; for your own class see Deconstruct methods. The element count must match an availableDeconstructoverload. - Single-element positional patterns are ambiguous with parentheses, so write
Type(pattern)or a property pattern instead. - Combine with logical patterns for ranges and alternatives:
{ Age: >= 13 and < 20 },{ Status: Status.New or Status.Open }. - Capture inside patterns with
varto avoid repeating the access:{ Address: { City: var city } }. - Pattern order and subsumption.
{ Age: < 18 }before{ Age: < 10 }makes the second arm unreachable and is a compile error. - Readability limit. When a pattern grows beyond two or three lines, a small method with a name is clearer.
- Added in: property patterns C# 8, extended property patterns C# 10, relational and logical patterns C# 9.