Skip to content

C# Feature - Property patterns

Property patterns let you inspect object state declaratively inside a pattern match instead of writing repeated null checks and member comparisons.

This feature was introduced in C# 8.0 (2019).

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 })
{
// ...
}
  • Use it when matching on object shape or member values is the main purpose of the branch.
  • Use it in switch expressions and decision-heavy business logic.
  • Prefer it when it removes repeated null checks and member access boilerplate.
if (order != null && order.Customer != null && order.Customer.IsVip)
{
ShipPriority(order);
}
if (order is { Customer.IsVip: true })
{
ShipPriority(order);
}
string label = order switch
{
{ Customer.Country: "IN", Total: > 1000m } => "priority",
{ Customer.Country: "US" } => "regional",
_ => "standard"
};
  • 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.
  • C# 8 introduced recursive patterns, including property patterns.
  • Later C# versions expanded related pattern-matching capabilities such as relational and list patterns.