Skip to content

Add example/documentation for 'is not' with non-nullable declaration pattern #26855

@marchy

Description

@marchy

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions