'is' versus try cast with null check

后端 未结 7 709
情歌与酒
情歌与酒 2020-12-08 03:28

I noticed that Resharper suggests that I turn this:

if (myObj.myProp is MyType)
{
   ...
}

into this:

var myObjRef = myObj.         


        
7条回答
  •  悲&欢浪女
    2020-12-08 03:58

    It should be suggesting a second change as well:

    (MyType)myObj.myProp
    

    into

    myObjRef
    

    This saves a property access and a cast, compared to the original code. But it's only possible after changing is to as.

提交回复
热议问题