115

我注意到 Resharper 建议我转这个:

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

进入这个:

var myObjRef = myObj.myProp as MyType;
if (myObjRef != null)
{
   ...
}

为什么它会建议这种变化?我习惯于 Resharper 建议优化更改和代码减少更改,但这感觉就像它想把我的单个语句变成一个两行。

根据MSDN

如果满足以下两个条件,则is 表达式的计算结果为 true:

表达式不为空。表达式可以转换为type。也就是说,表单的强制转换表达式(type)(expression)将在不引发异常的情况下完成。

我是否误读了这一点,或者没有is进行完全相同的检查,只是在一行中而不需要为空检查显式创建另一个局部变量?

4

7 回答 7

169

因为只有一个演员。比较一下:

if (myObj.myProp is MyType) // cast #1
{
    var myObjRef = (MyType)myObj.myProp; // needs to be cast a second time
                                         // before using it as a MyType
    ...
}

对此:

var myObjRef = myObj.myProp as MyType; // only one cast
if (myObjRef != null)
{
    // myObjRef is already MyType and doesn't need to be cast again
    ...
}

C# 7.0 使用模式匹配支持更紧凑的语法:

if (myObj.myProp is MyType myObjRef)
{
    ...
}
于 2012-11-15T20:40:32.357 回答
12

最好的选择是使用这样的模式匹配:

if (value is MyType casted){
    //Code with casted as MyType
    //value is still the same
}
//Note: casted can be used outside (after) the 'if' scope, too
于 2017-09-15T09:04:46.213 回答
7

目前还没有关于腰带下方实际发生的情况的信息。看看这个例子:

object o = "test";
if (o is string)
{
    var x = (string) o;
}

这转化为以下 IL:

IL_0000:  nop         
IL_0001:  ldstr       "test"
IL_0006:  stloc.0     // o
IL_0007:  ldloc.0     // o
IL_0008:  isinst      System.String
IL_000D:  ldnull      
IL_000E:  cgt.un      
IL_0010:  stloc.1     
IL_0011:  ldloc.1     
IL_0012:  brfalse.s   IL_001D
IL_0014:  nop         
IL_0015:  ldloc.0     // o
IL_0016:  castclass   System.String
IL_001B:  stloc.2     // x
IL_001C:  nop         
IL_001D:  ret   

这里重要的是isinstandcastclass调用——两者都相对昂贵。如果将其与替代方案进行比较,您会发现它仅进行isinst检查:

object o = "test";
var oAsString = o as string;
if (oAsString != null)
{

}

IL_0000:  nop         
IL_0001:  ldstr       "test"
IL_0006:  stloc.0     // o
IL_0007:  ldloc.0     // o
IL_0008:  isinst      System.String
IL_000D:  stloc.1     // oAsString
IL_000E:  ldloc.1     // oAsString
IL_000F:  ldnull      
IL_0010:  cgt.un      
IL_0012:  stloc.2     
IL_0013:  ldloc.2     
IL_0014:  brfalse.s   IL_0018
IL_0016:  nop         
IL_0017:  nop         
IL_0018:  ret  

另外值得一提的是值类型将使用unbox.any而不是castclass

object o = 5;
if (o is int)
{
    var x = (int)o;
}

IL_0000:  nop         
IL_0001:  ldc.i4.5    
IL_0002:  box         System.Int32
IL_0007:  stloc.0     // o
IL_0008:  ldloc.0     // o
IL_0009:  isinst      System.Int32
IL_000E:  ldnull      
IL_000F:  cgt.un      
IL_0011:  stloc.1     
IL_0012:  ldloc.1     
IL_0013:  brfalse.s   IL_001E
IL_0015:  nop         
IL_0016:  ldloc.0     // o
IL_0017:  unbox.any   System.Int32
IL_001C:  stloc.2     // x
IL_001D:  nop         
IL_001E:  ret   

但是请注意,这不一定会转化为更快的结果,正如我们在此处看到的那样。自从提出这个问题以来,似乎有了一些改进:演员表的执行速度似乎和以前一样快,但as现在linq快了大约 3 倍。

于 2016-04-05T15:11:18.030 回答
4

更清晰的警告:

"Type check and direct cast can be replaced with try cast and check for null"

两者都可以,这取决于您的代码如何更适合您。就我而言,我只是忽略了该警告:

//1st way is n+1 times of casting
if (x is A) ((A)x).Run();
else if (x is B) ((B)x).Run();
else if (x is C) ((C)x).Run();
else if (x is D) ((D)x).Run();
//...
else if (x is N) ((N)x).Run();    
//...
else if (x is Z) ((Z)x).Run();

//2nd way is z times of casting
var a = x as Type A;
var b = x as Type B;
var c = x as Type C;
//..
var n = x as Type N;
//..
var z = x as Type Z;
if (a != null) a.Run();
elseif (b != null) b.Run();
elseif (c != null) c.Run();
...
elseif (n != null) n.Run();
...
elseif (x != null) x.Run();

在我的代码中,第二种方式更长且性能更差。

于 2014-01-12T04:00:10.590 回答
3

对我来说,这似乎取决于它是否属于那种类型的可能性。如果对象大部分时间都是那种类型,那么预先进行转换肯定会更有效。如果只是偶尔出现这种类型,那么首先检查 is 可能会更理想。

与类型检查的成本相比,创建局部变量的成本可以忽略不计。

可读性和范围对我来说通常是更重要的因素。我不同意 ReSharper,仅出于这个原因使用“is”运算符;如果这是一个真正的瓶颈,请稍后优化。

(我假设你myObj.myProp is MyType在这个函数中只使用一次)

于 2012-11-15T20:46:03.833 回答
1

我会说这是制作 myObj.myProp 的强类型版本,即 myObjRef。当您在块中引用此值时,应该使用它,而不是必须进行强制转换。

例如,这个:

myObjRef.SomeProperty

比这更好:

((MyType)myObj.myProp).SomeProperty
于 2012-11-15T20:38:17.910 回答
0

它也应该建议进行第二次更改:

(MyType)myObj.myProp

进入

myObjRef

与原始代码相比,这节省了属性访问和强制转换。但这只有在更改is为之后才有可能as

于 2012-11-15T20:35:52.787 回答