我目前正在编写一个针对.NET 4.7(C#7)的C#应用程序.我尝试使用"is"关键字声明变量的新方法后感到困惑:
if (variable is MyClass classInstance)
这种方式有效,但在执行时:
if (true & variable is MyClass classInstance)
{
var a = classInstance;
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio(我使用的是2017)向我展示了错误Use of unassigned local variable 'classInstance'.使用&(&&)的短循环版本,它工作正常.我错过了关于&操作员的事情吗?(我知道使用shortcircuting版本更常用,但此时我只是好奇)