|
| 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: 01/27/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=fullName> property was previously annotated as nullable purely because the `PatternTestResult.Stem` property it gets its value from is nullable. While the `PatternTestResult` can indeed be null if the result is not successful, the `FilePatternMatch` never is because it is only ever constructed when `PatternTestResult` is successful. To accurately reflect nullability, the `FilePatternMatch.Stem` property is now non-nullable and the constructor parameter has been changed accordingly. |
| 12 | + |
| 13 | +## Version introduced |
| 14 | + |
| 15 | +.NET 10 RC 1 |
| 16 | + |
| 17 | +## Previous behavior |
| 18 | + |
| 19 | +The <xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch> constructor would accept a `null` for the `stem` parameter without any compile-time or run-time warnings or errors. The <xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch.Stem?displayProperty=fullName> property was also annotated as being nullable (`string?`). |
| 20 | + |
| 21 | +```csharp |
| 22 | +// This was allowed in previous versions |
| 23 | +var match = new FilePatternMatch("path/to/file.txt", null); |
| 24 | +string? stem = match.Stem; // Could be null |
| 25 | +``` |
| 26 | + |
| 27 | +## New behavior |
| 28 | + |
| 29 | +Passing a `null` or possibly-null value to the `stem` argument in the <xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch> constructor will yield a compile-time warning, and if `null` is passed in, a run-time <xref:System.ArgumentNullException> will be thrown. The <xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch.Stem?displayProperty=fullName> property is now annotated as non-nullable (`string`). |
| 30 | + |
| 31 | +```csharp |
| 32 | +// This now throws ArgumentNullException at runtime |
| 33 | +var match = new FilePatternMatch("path/to/file.txt", null); // Throws! |
| 34 | +
|
| 35 | +// The Stem property is now non-nullable |
| 36 | +string stem = match.Stem; // No null check needed |
| 37 | +``` |
| 38 | + |
| 39 | +## Type of breaking change |
| 40 | + |
| 41 | +This change can affect [source compatibility](../../categories.md#source-compatibility) and is a [behavioral change](../../categories.md#behavioral-change). |
| 42 | + |
| 43 | +## Reason for change |
| 44 | + |
| 45 | +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. The `[MemberNotNullWhen()]` attribute is applied to the `PatternTestResult.Stem` property to tell the compiler it will not be null if it is successful. |
| 46 | + |
| 47 | +## Recommended action |
| 48 | + |
| 49 | +If a possibly-null value was passed in for the `stem` argument, review usage and update the call site to ensure `stem` cannot be passed in as `null`. If nullability warning suppressions were applied when consuming the `FilePatternMatch.Stem` property, such suppressions can be removed. |
| 50 | + |
| 51 | +```csharp |
| 52 | +// Before: Check for null |
| 53 | +string? stem = match.Stem; |
| 54 | +if (stem != null) |
| 55 | +{ |
| 56 | + // Use stem |
| 57 | +} |
| 58 | + |
| 59 | +// After: No null check needed |
| 60 | +string stem = match.Stem; |
| 61 | +// Use stem directly |
| 62 | +``` |
| 63 | + |
| 64 | +## Affected APIs |
| 65 | + |
| 66 | +- <xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch.%23ctor(System.String,System.String)?displayProperty=fullName> |
| 67 | +- <xref:Microsoft.Extensions.FileSystemGlobbing.FilePatternMatch.Stem?displayProperty=fullName> |
0 commit comments