Skip to content

Commit fd34d69

Browse files
authored
Add example of { } pattern (#44544)
Fixes #43926 You can match a property against the `{ }` pattern to match any non-null property. Add an example of that construct.
1 parent cc9e280 commit fd34d69

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

docs/csharp/fundamentals/functional/pattern-matching.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Pattern matching overview
33
description: "Learn about pattern matching expressions in C#"
4-
ms.date: 03/13/2024
4+
ms.date: 01/27/2025
55
---
66

77
# Pattern matching overview
@@ -78,6 +78,14 @@ The first two arms examine two properties of the `Order`. The third examines onl
7878

7979
The preceding code demonstrates the [*positional pattern*](../../language-reference/operators/patterns.md#positional-pattern) where the properties are deconstructed for the expression.
8080

81+
You can also match a property against `{ }`, which matches any non-null value. Consider the following declaration, which stores measurements with an optional annotation:
82+
83+
:::code language="csharp" source="snippets/patterns/Program.cs" ID="Observation":::
84+
85+
You can test if a given observation has a non-null annotation using the following pattern matching expression:
86+
87+
:::code language="csharp" source="snippets/patterns/Program.cs" ID="NotNullPropertyPattern":::
88+
8189
## List patterns
8290

8391
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.

docs/csharp/fundamentals/functional/snippets/patterns/Program.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ static void Main(string[] args)
88

99
NullReferenceCheck();
1010

11-
var sequence = new List<int> {1,2,3,4,5,6,7};
11+
var sequence = new List<int> { 1, 2, 3, 4, 5, 6, 7 };
1212
var middle = MidPoint(sequence);
1313
Console.WriteLine(middle);
1414

1515
ListPattern.Example();
16+
17+
NotNullProperty(new Observation(42, "C", "Temperature"));
1618
}
1719

1820
// <MidPoint>
@@ -65,4 +67,21 @@ private static void NullCheck()
6567
}
6668
// </NullableCheck>
6769
}
70+
71+
private static void NotNullProperty(Observation observation)
72+
{
73+
// <NotNullPropertyPattern>
74+
if (observation.Annotation is { })
75+
{
76+
Console.WriteLine($"Observation description: {observation.Annotation}");
77+
}
78+
// </NotNullPropertyPattern>
79+
}
80+
81+
// <Observation>
82+
public record class Observation(int Value, string Units, string Name)
83+
{
84+
public string? Annotation { get; set; }
85+
}
86+
// </Observation>
6887
}

docs/csharp/fundamentals/functional/snippets/patterns/patterns.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
</PropertyGroup>

0 commit comments

Comments
 (0)