diff --git a/docs/csharp/fundamentals/functional/pattern-matching.md b/docs/csharp/fundamentals/functional/pattern-matching.md index a3b24fc943169..9754d42a1ca81 100644 --- a/docs/csharp/fundamentals/functional/pattern-matching.md +++ b/docs/csharp/fundamentals/functional/pattern-matching.md @@ -1,7 +1,7 @@ --- title: Pattern matching overview description: "Learn about pattern matching expressions in C#" -ms.date: 03/13/2024 +ms.date: 01/27/2025 --- # Pattern matching overview @@ -78,6 +78,14 @@ The first two arms examine two properties of the `Order`. The third examines onl The preceding code demonstrates the [*positional pattern*](../../language-reference/operators/patterns.md#positional-pattern) where the properties are deconstructed for the expression. +You can also match a property against `{ }`, which matches any non-null value. Consider the following declaration, which stores measurements with an optional annotation: + +:::code language="csharp" source="snippets/patterns/Program.cs" ID="Observation"::: + +You can test if a given observation has a non-null annotation using the following pattern matching expression: + +:::code language="csharp" source="snippets/patterns/Program.cs" ID="NotNullPropertyPattern"::: + ## List patterns You can check elements in a list or an array using a *list pattern*. A [list pattern](../../language-reference/operators/patterns.md#list-patterns) provides a means to apply a pattern to any element of a sequence. In addition, you can apply the *discard pattern* (`_`) to match any element, or apply a *slice pattern* to match zero or more elements. diff --git a/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs b/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs index 6045d2b9de9f3..5e61209361470 100644 --- a/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs +++ b/docs/csharp/fundamentals/functional/snippets/patterns/Program.cs @@ -8,11 +8,13 @@ static void Main(string[] args) NullReferenceCheck(); - var sequence = new List {1,2,3,4,5,6,7}; + var sequence = new List { 1, 2, 3, 4, 5, 6, 7 }; var middle = MidPoint(sequence); Console.WriteLine(middle); ListPattern.Example(); + + NotNullProperty(new Observation(42, "C", "Temperature")); } // @@ -65,4 +67,21 @@ private static void NullCheck() } // } + + private static void NotNullProperty(Observation observation) + { + // + if (observation.Annotation is { }) + { + Console.WriteLine($"Observation description: {observation.Annotation}"); + } + // + } + + // + public record class Observation(int Value, string Units, string Name) + { + public string? Annotation { get; set; } + } + // } diff --git a/docs/csharp/fundamentals/functional/snippets/patterns/patterns.csproj b/docs/csharp/fundamentals/functional/snippets/patterns/patterns.csproj index f704bf4988fa6..58be7ac77af37 100644 --- a/docs/csharp/fundamentals/functional/snippets/patterns/patterns.csproj +++ b/docs/csharp/fundamentals/functional/snippets/patterns/patterns.csproj @@ -2,7 +2,7 @@ Exe - net8.0 + net9.0 enable enable