Allow variable for "is not null" pattern #6322
-
Please take a look at the following code: static Version? TryGetVersion() => new(1, 0);
if (TryGetVersion() is { } version)
Console.WriteLine(version);
else
Console.WriteLine("<null>"); It calls The code does the job in a concise way but is ugly because The code below would produce a much more readable result immediately communicating the programmer's intent: static Version? TryGetVersion() => new(1, 0);
if (TryGetVersion() is not null version)
Console.WriteLine(version);
else
Console.WriteLine("<null>"); Unfortunately, |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Beta Was this translation helpful? Give feedback.
-
You can do |
Beta Was this translation helpful? Give feedback.
-
I would also find this a useful feature! The syntax: |
Beta Was this translation helpful? Give feedback.
-
I find this worthwhile adding in the language for the same reason we got the shorthand for |
Beta Was this translation helpful? Give feedback.
You can do
if (TryGetVersion() is var version and not null)
.