If I have an if statement like if (currentShape is ITable table || currentShape is AutoShape autoShape)
I cannot use table
or autoShape
in the body because I get a CS0165 compiler error.
The same is true for a switch statement with fall-through:
void Foo(object o)
{
switch (o)
{
case int i:
case string s:
case Guid g:
string bar = i?.ToString() ?? s?.ToString() ?? g.ToString(); // cannot use i, s, or g.
break;
}
}
I understand why, but I'm left wondering, is this a limitation of pattern matching, i.e. you cannot use it in compound if
statements, or is there a correct way to construct the statement so I can use either variable (e.g. by initializing them to null so I can then at least do a null check)?