Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion standard/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@
;
subpattern
: pattern
| identifier ':' pattern
| subpattern_name ':' pattern
;
subpattern_name
: identifier
| subpattern_name '.' identifier
;
```

Expand Down Expand Up @@ -319,6 +323,12 @@

The *property_pattern* may be used to pattern-match with anonymous types.

A *property_subpattern* may reference a nested member. In such a case, the receiver for each name lookup is the type of the previous member *T₀*, starting from the *input type* of the *property_pattern*. If *T* is a nullable type, *T₀* is its underlying type, otherwise *T₀* is equal to *T*. For example, a pattern of the form `{ Prop1.Prop2: pattern }` is exactly equivalent to `{ Prop1: { Prop2: pattern } }`.

> *Note*: This will include the null check when *T* is a nullable value type or a reference type. This null check means that the nested properties available will be the properties of *T₀*, not of *T*. As repeated member paths are allowed, the compilation of pattern matching can take advantage of common parts of patterns. *end note*
<!-- markdownlint-disable MD028 -->

<!-- markdownlint-enable MD028 -->
> *Example*:
>
> <!-- Example: {template:"standalone-console", name:"PropertyPattern2", replaceEllipsis:true, customEllipsisReplacements: ["new object()", ";"], ignoredWarnings:["CS0642"]} -->
Expand All @@ -336,14 +346,14 @@
> <!-- Example: {template:"standalone-console", name:"PropertyPattern3", inferOutput:true} -->
> ```csharp
> Console.WriteLine(TakeFive("Hello, world!")); // output: Hello
> Console.WriteLine(TakeFive("Hi!")); // output: Hi!

Check warning on line 349 in standard/patterns.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/patterns.md#L349

MDC032::Line length 91 > maximum 81
> Console.WriteLine(TakeFive(new[] { '1', '2', '3', '4', '5', '6', '7' })); // output: 12345
> Console.WriteLine(TakeFive(new[] { 'a', 'b', 'c' })); // output: abc
>
> static string TakeFive(object input) => input switch
> {
> string { Length: >= 5 } s => s.Substring(0, 5),
> string s => s,

Check warning on line 356 in standard/patterns.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/patterns.md#L356

MDC032::Line length 87 > maximum 81
> ICollection<char> { Count: >= 5 } symbols => new string(symbols.Take(5).ToArray()),
> ICollection<char> symbols => new string(symbols.ToArray()),
> null => throw new ArgumentNullException(nameof(input)),
Expand Down
Loading