Why does tuple deconstruction return nullable types in C#?

54 views Asked by At

Consider the following C# code:

var info = ("A", "B");
if (info is var (a, b))
{
    Console.WriteLine("X = {0}", a);
}

For some reason, the type of the a variable is inferred as optional (string?), while neither the whole tuple nor its elements can be nullable. At the same time, Visual Studio hints that a cannot be null within the condition's block:

Inference result

Is there any particular reason why type inference should work like this here?

0

There are 0 answers