Let's have string foo variable in C#.
Why are the following statements not semantically equivalent?
if (!(foo is { } bar) || bar.Length == 0)
and
if (foo is not { } bar || bar.Length == 0)
For foo = null, both appear to behave identically.
var foo = null;
if (!(foo is { } bar) || bar.Length == 0)
{
Console.WriteLine("bar");
}
if (foo is not { } baz || baz.Length == 0)
{
Console.WriteLine("baz");
}
But for foo = "something", the first statement works, but the second fails with NullReferenceException.
Check sharplab.
Is it a feature?
Edit: It seems that it was a bug: https://github.com/dotnet/roslyn/issues/51996#issuecomment-803508111