1
int? a = null;
var b = (a is (int?) y) ? "bla bla bla" : y.ToString();

The name y does not exist in the current context.

1 Answers1

4

No, this is not supported.

If you look at the MS documentation here:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types#how-to-identify-a-nullable-value-type

Then down a little it states

Also, don't use the is operator to determine whether an instance is of a nullable value type. As the following example shows, you cannot distinguish types of a nullable value type instance and its underlying type instance with the is operator:

it says you should

Instead use the Nullable.GetUnderlyingType from the first example and typeof operator to check if an instance is of a nullable value type.

See also the answer here, which provides a nice method IsNullable

How to check if an object is nullable?

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51