-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
Please add an example/section in the 'Declaration and type patterns' section to showcase the use of a non-nullable declaration pattern with 'is not' use case, which adds the equivalent functionality of Swift's guard let
, discussed in feature request here.
if (someObject.SomeProperty is not string someProperty) {
return; // or 'continue'
}
While the positive case is covered with this example: if (greeting is string message) { .. }
, the inverse is non-obvious and opens up very useful null-safe short-circuiting control flows that are not stated anywhere in the documentation. The only current example is if (input is not null) { .. }
in the 'Constant pattern' and 'Logical patterns' sections, but that does not combine a Declaration pattern within it – which is what brings all the extra usefulness.
The existence of this feature is a major component of writing modern, null-safe, idiomatic C#, supported since C# 9, let's get the word out about it!
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 9112c305-f1cd-1aa5-4557-2c7d757e6ca0
- Version Independent ID: 312e9c81-ee2a-1d84-64d7-bcaa71d7eab8
- Content: Patterns - C# reference
- Content Source: docs/csharp/language-reference/operators/patterns.md
- Product: dotnet-csharp
- Technology: csharp-language-reference
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn