Ziv*_*ivS 6 .net c# compiler-errors operators
为什么(expr is type varname) == false会出现编译错误,但可以!(expr is type varname)编译?
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'
}
Run Code Online (Sandbox Code Playgroud)
现场示例:https : //dotnetfiddle.net/nYF7b6