我注意到 Resharper 建议我转动这个:
if (myObj.myProp is MyType)
{
...
}
变成这样:
var myObjRef = myObj.myProp as MyType;
if (myObjRef != null)
{
...
}
为什么会有这样的变化?我习惯于 Resharper 建议优化变更和代码缩减变更,但这感觉像是它想把我的单个语句变成一个两行程序。
根据 MSDN:
如果满足以下两个条件,则 是 表情的计算结果为 true 满足以下条件:
表达式 不为空。表达式可以强制转换为 类型 形式
(type)(expression)
的强制转换表达式将在没有 抛出一个例外。
是我理解错了,还是 is
没有做完全相同的检查,只是在一行中没有显式地创建另一个局部变量来进行 null 检查?