skip to content

In C#, what does the extended property pattern `{ Employment.Years: >= 2 }` mean, and what happens when Employment is null?

level: middleimportance: should knowfreq 45%

answer

  1. C# 10 shorthand
  2. dots instead of nested braces
  3. a null test at every step
  4. nullable structs unwrap on the path

basics

~20 s

It is C# 10 shorthand for the nested pattern { Employment: { Years: >= 2 } }: Employment must be non-null and its Years at least 2. A null Employment makes the pattern false; nothing throws.

solid answer

~40 s

C# 10 added **extended property patterns**: a subpattern name can be a dotted member path, and `{ Employment.Years: >= 2 }` is exactly `{ Employment: { Years: >= 2 } }`. Each step keeps the property pattern's non-null test, so a null `Employment` makes the match `false` instead of throwing. A step through a nullable value type unwraps it, so `{ Employment.Since.Year: < 2022 }` works on a `DateOnly?` without `.Value`. Negation is where it surprises people: `{ Address.Country: not "FR" }` is `false` for a null `Address`, while `a.Address?.Country != "FR"` is `true`. The IDE offers the dotted form through the IDE0170 style rule.

code

csharp · 16 lines
csharp
var noJob = new Applicant(30, null, new Address("Lyon", "FR"));
var hired = new Applicant(30, new Employment("Acme", 4, new DateOnly(2021, 3, 1)), null);

Console.WriteLine(noJob is { Employment.Years: >= 2 });          // False, no exception
Console.WriteLine(hired is { Employment.Years: >= 2 });          // True
Console.WriteLine(hired is { Employment: { Years: >= 2 } });     // True, the C# 8 nested form
Console.WriteLine(hired is { Employment.Since.Year: < 2022 });   // True, no .Value on DateOnly?

// A null Address fails the pattern, so "not FR" is false here...
Console.WriteLine(hired is { Address.Country: not "FR" });       // False
// ...while the null-conditional comparison is true
Console.WriteLine(hired.Address?.Country != "FR");               // True

record Applicant(int Age, Employment? Employment, Address? Address);
record Employment(string Employer, int Years, DateOnly? Since);
record Address(string City, string Country);

go deeper

for a junior

Recognise { Employment.Years: >= 2 } as a short form of nested braces, and remember that a null step makes the match false.

for a middle

Explain that every dot carries a non-null test, that nullable value types unwrap on the path, and why not under a dotted path differs from a ?. comparison.

for a senior

Catch rules where a null step should count as a match, rewrite them with null or, and flag getters with side effects that patterns read in an unspecified order.

for a principal

Agree a team convention for dotted versus nested patterns and enforce it with IDE0170, so rule code reads the same across services.

## From nested braces to a member path A **property pattern** names members of its input and matches each one against a nested pattern. Before C# 10, reaching a member of a member meant nesting another property pattern: ```csharp applicant is { Employment: { Years: >= 2 } } ``` C# 10 lets the subpattern name be a dotted path instead, an **extended property pattern**: ```csharp applicant is { Employment.Years: >= 2 } ``` The C# 10 proposal defines `{ Prop1.Prop2: pattern }` as exactly equivalent to `{ Prop1: { Prop2: pattern } }`. It is purely a shorter spelling: the same tests run, the same null checks happen, and the same value reaches the final pattern. A project on C# 9 or earlier rejects it with CS8773, "Feature 'extended property patterns' is not available in C# 9.0. Please use language version 10.0 or greater." ## Null at any step Because each dot stands for a nested property pattern, each step carries that pattern's non-null test. For `{ Employment.Years: >= 2 }`: 1. The applicant must be non-null. 2. `Employment` is read and must be non-null. 3. `Years` is read and matched against `>= 2`. If any step is null, the match is `false` and nothing throws. A loan rule that checks an employment record therefore needs no separate `Employment != null` guard. The same holds in a switch expression: an arm such as `{ Employment.Years: >= 2 } => "approve"` simply does not match an applicant without an employment record, and the next arm is tried. ## Paths through nullable value types When a step's type is a nullable value type such as `DateOnly?`, the next name is looked up on the underlying type. `{ Employment.Since.Year: < 2022 }` reads `Since`, checks it has a value, and reads `Year` from the `DateOnly` inside. Writing `.Value` in the path is an error, CS0117, "'DateOnly' does not contain a definition for 'Value'", because the path is already on `DateOnly`. ## Negation and the null-conditional operator differ The dotted path looks like the `?.` operator, but the two disagree whenever negation meets a null step: | Expression | Address is null | Country is "DE" | Country is "FR" | |---|---|---|---| | `a is { Address.Country: not "FR" }` | false | true | false | | `a.Address?.Country != "FR"` | true | true | false | | `a is { Address: null or { Country: not "FR" } }` | true | true | false | The pattern needs a non-null `Address` before it can test `Country`, so a null `Address` fails the whole pattern and `not "FR"` is never evaluated. The `?.` expression turns a null `Address` into a null string, and `null != "FR"` is `true`. Decide which answer the rule wants and write it explicitly, as the last row does. ## Repeated paths and getter calls Several subpatterns may share a prefix, as in `{ Address.City: "Lyon", Address.Country: "FR" }`. The proposal notes that the compiler can take advantage of common parts of patterns, and the language design permits the compiler to reorder property reads and assume a getter returns the same value each time. With the .NET 10 SDK, a logging `Address` getter runs once for that pattern, and once for a whole switch expression that tests `Address.Country` in several arms. Do not depend on either count: - keep getters that patterns read free of side effects; - do not expect subpatterns to run in the order they are written; - do not expect a failed match to have read every member. ## Rules and limits - A path is identifiers joined by dots: properties or fields, accessible and readable at that point. `?.` in the name is CS8918, "Identifier or a simple member access expected", and the path cannot call a method or an indexer. - A dotted name always starts at the pattern's input. `{ Employment: not null, Years: >= 2 }` looks for `Years` on the applicant and fails with CS0117 when it is not there. - Any pattern can sit at the end of the path: constants, relational patterns, `and`/`or`/`not`, or another property pattern. - The IDE0170 style rule, "Simplify property pattern", flags a nested pattern inside a property pattern that the dotted form can replace; its option `csharp_style_prefer_extended_property_pattern` defaults to `true`.

  • Why do { Address.Country: not "FR" } and a.Address?.Country != "FR" disagree for a null Address?
    The pattern needs a non-null `Address` before it can test `Country`, so a null `Address` fails the whole pattern and the result is `false`. The `?.` expression turns the null `Address` into a null string, and `null != "FR"` is `true`. If a missing address should count as not French, write `{ Address: null or { Country: not "FR" } }`.
  • Does { Address.City: "Lyon", Address.Country: "FR" } read the Address property twice?
    The language leaves that open: the compiler may share the common path, reorder reads and assume a getter returns the same value each time. With the .NET 10 SDK a logging getter runs once for that pattern. Treat the count as unspecified and keep getters that patterns read free of side effects.
  • What happens to a dotted subpattern in a project compiled as C# 9?
    It fails with CS8773, "Feature 'extended property patterns' is not available in C# 9.0. Please use language version 10.0 or greater." The nested form `{ Employment: { Years: >= 2 } }` compiles from C# 8, so rewrite it that way or raise the language version.

  • { Employment.Years: >= 2 } throws NullReferenceException when Employment is null.
  • A dotted path through a DateOnly? member needs .Value in the middle.
  • { Address.Country: not "FR" } matches applicants who have no address at all.
  • An extended property pattern is just another spelling of the ?. operator.
  • Each repeated path segment calls the getter again, in the order written.