一直都以为这两者之间的三目运算符是没有区别的,只是vb是用iif,c#是?:而已。但是上次用了一次vb的iif之后却发现两者之间简直是天渊之别。先看下面的代码
Sub TestIIF()
Dim d As DBNull = DBNull.Value
Dim k As Integer = IIf(TypeOf d Is DBNull, 0, Convert.ToInt32(d))
Console.WriteLine(k)
End Sub
IL如下:
.method public static void TestIIF() cil managed
{
// 代码大小 53 (0x35)
.maxstack 3
.locals init ([0] class [mscorlib]System.DBNull d,
[1] int32 k)
IL_0000: nop
IL_0001: ldsfld class [mscorlib]System.DBNull [mscorlib]System.DBNull::Value
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: isinst [mscorlib]System.DBNull
IL_000d: ldnull
IL_000e: cgt.un
IL_0010: ldc.i4.0
IL_0011: box [mscorlib]System.Int32
IL_0016: ldloc.0
IL_0017: call int32 [mscorlib]System.Convert::ToInt32(object)
IL_001c: box [mscorlib]System.Int32
IL_0021: call object [Microsoft.VisualBasic]Microsoft.VisualBasic.Interaction::IIf(bool,
object,
object)
IL_0026: call int32 [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToInteger(object)
IL_002b: stloc.1
IL_002c: ldloc.1
IL_002d: call void [mscorlib]System.Console::WriteLine(int32)
IL_0032: nop
IL_0033: nop
IL_0034: ret
} // end of method Module1::TestIIF
从il可以看出vb的iif是将前后两个表达是计算出来然后装相成object,然后再调用
object [Microsoft.VisualBasic]Microsoft.VisualBasic.Interaction::IIf(bool,object,object)。
所以如果你要执行以下语句
Dim k As Integer = IIf(TypeOf d Is DBNull, 0, Convert.ToInt32(d))编译器会直接报错,因为要先将dbnull转化为int装箱。
再来看一下c#的三目运算符
代码如下
static void TestThreeMU()
{
DBNull d = DBNull.Value;
int k = d.GetType() == typeof(DBNull) ? 1 : Convert.ToInt32(DBNull.Value);
Console.WriteLine(k);
}
IL如下
.method private hidebysig static void TestThreeMU() cil managed
{
// 代码大小 47 (0x2f)
.maxstack 2
.locals init ([0] class [mscorlib]System.DBNull d,
[1] int32 k)
IL_0000: nop
IL_0001: ldsfld class [mscorlib]System.DBNull [mscorlib]System.DBNull::Value
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: callvirt instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
IL_000d: ldtoken [mscorlib]System.DBNull
IL_0012: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_0017: beq.s IL_0025
IL_0019: ldsfld class [mscorlib]System.DBNull [mscorlib]System.DBNull::Value
IL_001e: call int32 [mscorlib]System.Convert::ToInt32(object)
IL_0023: br.s IL_0026
IL_0025: ldc.i4.1
IL_0026: stloc.1
IL_0027: ldloc.1
IL_0028: call void [mscorlib]System.Console::WriteLine(int32)
IL_002d: nop
IL_002e: ret
} // end of method Program::TestThreeMU
从IL中我们可以看见C#是直接将三目运算符编译成一个bgt.s。所以
int k = d.GetType() == typeof(DBNull) ? 1 : Convert.ToInt32(DBNull.Value);这一句代码并不会出错,因为IL跳到了IL_0025执行并不会执行转换代码。