C# Feature - Property patterns
Property patterns
Section titled โProperty patternsโProperty patterns let you inspect object state declaratively inside a pattern match instead of writing repeated null checks and member comparisons.
Introduced In
Section titled โIntroduced InโThis feature was introduced in C# 8.0 (2019).
Why This Feature Exists
Section titled โWhy This Feature ExistsโTraditional object inspection often grows into deeply nested if conditions. Property patterns move that structural matching into the pattern itself, which keeps branching logic shorter and easier to scan.
if (value is { Property: pattern }){ // ...}When To Use It
Section titled โWhen To Use Itโ- Use it when matching on object shape or member values is the main purpose of the branch.
- Use it in
switchexpressions and decision-heavy business logic. - Prefer it when it removes repeated
nullchecks and member access boilerplate.
if (order != null && order.Customer != null && order.Customer.IsVip){ ShipPriority(order);}if (order is { Customer.IsVip: true }){ ShipPriority(order);}Practical Example
Section titled โPractical Exampleโstring label = order switch{ { Customer.Country: "IN", Total: > 1000m } => "priority", { Customer.Country: "US" } => "regional", _ => "standard"};Common Pitfalls
Section titled โCommon Pitfallsโ- Deeply nested patterns can become harder to read than a well-named helper method.
- Use them for matching, not for embedding complicated business rules into a single expression.
- Keep an eye on nullability and result coverage when switching over reference graphs.
Version Notes
Section titled โVersion Notesโ- C# 8 introduced recursive patterns, including property patterns.
- Later C# versions expanded related pattern-matching capabilities such as relational and list patterns.