Why does (expr is type varname) == false gives a compile error, but !(expr is type varname) compiles?
public static void Foo(object o)
{
if(!(o is string s)) // <-- Using '!'
{
return;
}
Console.WriteLine(s); // <-- OK
}
public static void Bar(object o)
{
if((o is string s) == false) // <-- Using '== false'
{
return;
}
Console.WriteLine(s); // <--Error: Use of unassigned local variable 's'
}
Live example: https://dotnetfiddle.net/nYF7b6