小编BMI*_*I24的帖子

非shortcircuting布尔运算符和C#7模式匹配

我目前正在编写一个针对.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版本更常用,但此时我只是好奇)

c# c#-7.0

10
推荐指数
2
解决办法
600
查看次数

标签 统计

c# ×1

c#-7.0 ×1