From e522f1500cc9fd52dd5785ff1b81be695f9d9510 Mon Sep 17 00:00:00 2001 From: Alexander Gayko Date: Tue, 21 Oct 2025 15:43:29 +0200 Subject: [PATCH 1/4] Update patterns.md with empty property pattern details Clarify the behavior of the empty property pattern in C#. --- docs/csharp/language-reference/operators/patterns.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/csharp/language-reference/operators/patterns.md b/docs/csharp/language-reference/operators/patterns.md index 2299ac2518eff..35baa4dfa2daa 100644 --- a/docs/csharp/language-reference/operators/patterns.md +++ b/docs/csharp/language-reference/operators/patterns.md @@ -182,6 +182,8 @@ You can also add a run-time type check and a variable declaration to a property :::code language="csharp" source="snippets/patterns/PropertyPattern.cs" id="WithTypeCheck"::: +This specifially means that the *empty* property pattern `is { }` matches everything non-null, and can be used instead of the `is not null` to create a variable: `somethingPossiblyNull is { } somethingDefinitelyNotNull`. + A property pattern is a recursive pattern. You can use any pattern as a nested pattern. Use a property pattern to match parts of data against nested patterns, as the following example shows: :::code language="csharp" source="snippets/patterns/PropertyPattern.cs" id="RecursivePropertyPattern"::: From 14c07537fdac8d9dcafb8bbf708c3db1326928cf Mon Sep 17 00:00:00 2001 From: Alexander Gayko Date: Wed, 29 Oct 2025 12:28:54 +0100 Subject: [PATCH 2/4] Add code example for empty property pattern Added a code example demonstrating the use of an empty property pattern with variable creation in C#. --- .../language-reference/operators/patterns.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/csharp/language-reference/operators/patterns.md b/docs/csharp/language-reference/operators/patterns.md index 35baa4dfa2daa..152735e213e10 100644 --- a/docs/csharp/language-reference/operators/patterns.md +++ b/docs/csharp/language-reference/operators/patterns.md @@ -183,7 +183,19 @@ You can also add a run-time type check and a variable declaration to a property :::code language="csharp" source="snippets/patterns/PropertyPattern.cs" id="WithTypeCheck"::: This specifially means that the *empty* property pattern `is { }` matches everything non-null, and can be used instead of the `is not null` to create a variable: `somethingPossiblyNull is { } somethingDefinitelyNotNull`. - +--------- @BillWagner here's a code example to be put into a `:::code:::` block: +```cs +if (GetSomeNullableStringValue() is { } nonNullValue) // Empty property pattern with variable creation +{ + Console.WriteLine("NotNull:" + nonNullValue) +} +else +{ + nonNullValue = "NullFallback"; // we can access the variable here. + Console.WriteLine("it was null, here's the fallback: " + nonNullValue) +} +``` +---------end of the example A property pattern is a recursive pattern. You can use any pattern as a nested pattern. Use a property pattern to match parts of data against nested patterns, as the following example shows: :::code language="csharp" source="snippets/patterns/PropertyPattern.cs" id="RecursivePropertyPattern"::: From 829cbfe9576ee6c5a8f07c735664b4d169367c38 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 29 Oct 2025 12:22:49 -0400 Subject: [PATCH 3/4] Move sample to snippets. Per conversation on #49385 --- .../language-reference/operators/patterns.md | 16 +++------------- .../snippets/patterns/PropertyPattern.cs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/docs/csharp/language-reference/operators/patterns.md b/docs/csharp/language-reference/operators/patterns.md index 152735e213e10..8294edc532368 100644 --- a/docs/csharp/language-reference/operators/patterns.md +++ b/docs/csharp/language-reference/operators/patterns.md @@ -183,19 +183,9 @@ You can also add a run-time type check and a variable declaration to a property :::code language="csharp" source="snippets/patterns/PropertyPattern.cs" id="WithTypeCheck"::: This specifially means that the *empty* property pattern `is { }` matches everything non-null, and can be used instead of the `is not null` to create a variable: `somethingPossiblyNull is { } somethingDefinitelyNotNull`. ---------- @BillWagner here's a code example to be put into a `:::code:::` block: -```cs -if (GetSomeNullableStringValue() is { } nonNullValue) // Empty property pattern with variable creation -{ - Console.WriteLine("NotNull:" + nonNullValue) -} -else -{ - nonNullValue = "NullFallback"; // we can access the variable here. - Console.WriteLine("it was null, here's the fallback: " + nonNullValue) -} -``` ----------end of the example + +:::code language="csharp" source="snippets/patterns/PropertyPattern.cs" id="EmptyPropertyPattern"::: + A property pattern is a recursive pattern. You can use any pattern as a nested pattern. Use a property pattern to match parts of data against nested patterns, as the following example shows: :::code language="csharp" source="snippets/patterns/PropertyPattern.cs" id="RecursivePropertyPattern"::: diff --git a/docs/csharp/language-reference/operators/snippets/patterns/PropertyPattern.cs b/docs/csharp/language-reference/operators/snippets/patterns/PropertyPattern.cs index 1c1be24b28836..c6ec2e031b1be 100644 --- a/docs/csharp/language-reference/operators/snippets/patterns/PropertyPattern.cs +++ b/docs/csharp/language-reference/operators/snippets/patterns/PropertyPattern.cs @@ -5,8 +5,27 @@ public static class PropertyPattern public static void Examples() { WithTypeCheck(); + + // + if (GetSomeNullableStringValue() is { } nonNullValue) // Empty property pattern with variable creation + { + Console.WriteLine("NotNull:" + nonNullValue); + } + else + { + nonNullValue = "NullFallback"; // we can access the variable here. + Console.WriteLine("it was null, here's the fallback: " + nonNullValue); + } + // + } + private static string? GetSomeNullableStringValue() + { + // Simulate getting a nullable string value. + return DateTime.Now.Ticks % 2 == 0 ? "Hello, World!" : null; + } + // static bool IsConferenceDay(DateTime date) => date is { Year: 2020, Month: 5, Day: 19 or 20 or 21 }; // From 322304c4ae4e17691630799e7875f9b1fe33c7c2 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Wed, 29 Oct 2025 12:23:20 -0400 Subject: [PATCH 4/4] Update docs/csharp/language-reference/operators/patterns.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- docs/csharp/language-reference/operators/patterns.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/language-reference/operators/patterns.md b/docs/csharp/language-reference/operators/patterns.md index 8294edc532368..0a9016daa7cc6 100644 --- a/docs/csharp/language-reference/operators/patterns.md +++ b/docs/csharp/language-reference/operators/patterns.md @@ -182,7 +182,7 @@ You can also add a run-time type check and a variable declaration to a property :::code language="csharp" source="snippets/patterns/PropertyPattern.cs" id="WithTypeCheck"::: -This specifially means that the *empty* property pattern `is { }` matches everything non-null, and can be used instead of the `is not null` to create a variable: `somethingPossiblyNull is { } somethingDefinitelyNotNull`. +This specifically means that the *empty* property pattern `is { }` matches everything non-null, and can be used instead of the `is not null` to create a variable: `somethingPossiblyNull is { } somethingDefinitelyNotNull`. :::code language="csharp" source="snippets/patterns/PropertyPattern.cs" id="EmptyPropertyPattern":::