|
| 1 | +--- |
| 2 | +title: "Breaking change - FilePatternMatch.Stem changed to non-nullable" |
| 3 | +description: "Learn about the breaking change in .NET 10 where FilePatternMatch.Stem property was changed from nullable to non-nullable." |
| 4 | +ms.date: 09/08/2025 |
| 5 | +ai-usage: ai-assisted |
| 6 | +ms.custom: https://github.com/dotnet/docs/issues/47914 |
| 7 | +--- |
| 8 | + |
| 9 | +# FilePatternMatch.Stem changed to non-nullable |
| 10 | + |
| 11 | +The <xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch.Stem?displayProperty=nameWithType> property was previously annotated as nullable because the `PatternTestResult.Stem` property it gets its value from is nullable. While the <xref:Microsoft.Extensions.FileSystemGlobbing.Internal.PatternTestResult> can indeed be null if the result isn't successful, the `FilePatternMatch` never is because it's only constructed when `PatternTestResult` is successful. |
| 12 | + |
| 13 | +To accurately reflect nullability, the `[MemberNotNullWhen()]` attribute is applied to the `PatternTestResult.Stem` property to tell the compiler it won't be null if it's successful. Additionally, the `stem` argument passed into the `FilePatternMatch` constructor is no longer nullable, and an `ArgumentNullException` will be thrown if a null `stem` is passed in. |
| 14 | + |
| 15 | +## Version introduced |
| 16 | + |
| 17 | +.NET 10 RC 1 |
| 18 | + |
| 19 | +## Previous behavior |
| 20 | + |
| 21 | +Previously, the <xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch> constructor accepted `null` for the `stem` parameter without any compile-time or run-time warnings or errors. |
| 22 | + |
| 23 | +```csharp |
| 24 | +// Allowed in previous versions. |
| 25 | +var match = new FilePatternMatch("path/to/file.txt", null); |
| 26 | +``` |
| 27 | + |
| 28 | +The <xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch.Stem?displayProperty=nameWithType> property was also annotated as being nullable. |
| 29 | + |
| 30 | +## New behavior |
| 31 | + |
| 32 | +Starting in .NET 10, passing a `null` or possibly-null value to the `stem` argument in the <xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch> constructor yields a compile-time warning. And, if `null` is passed in, a run-time <xref:System.ArgumentNullException> is thrown. |
| 33 | + |
| 34 | +The <xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch.Stem?displayProperty=nameWithType> property is now annotated to indicate that the value won't be null if `IsSuccessful` is `true`. |
| 35 | + |
| 36 | +```csharp |
| 37 | +// Generates compile-time warning. |
| 38 | +var match = new FilePatternMatch("path/to/file.txt", null); |
| 39 | +``` |
| 40 | + |
| 41 | +## Type of breaking change |
| 42 | + |
| 43 | +This change can affect [source compatibility](../../categories.md#source-compatibility) and is a [behavioral change](../../categories.md#behavioral-change). |
| 44 | + |
| 45 | +## Reason for change |
| 46 | + |
| 47 | +The previous nullability annotations were inaccurate, and a `null` value for the `stem` argument was unexpected but not properly guarded against. This change reflects the expected behavior of the API and guards against unpredictable behavior while also producing design-time guidance around usage. |
| 48 | + |
| 49 | +## Recommended action |
| 50 | + |
| 51 | +If a possibly null value was passed in for the `stem` argument, review usage and update the call site to ensure `stem` can't be paTssed in as `null`. |
| 52 | + |
| 53 | +If you applied nullability warning suppressions when consuming the `FilePatternMatch.Stem` property, you can remove those suppressions. |
| 54 | + |
| 55 | +## Affected APIs |
| 56 | + |
| 57 | +- [FilePatternMatch(String,String) constructor](xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch.%23ctor(System.String,System.String)) |
| 58 | +- <xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch.Stem?displayProperty=fullName> |
0 commit comments