vb.net 和c#下的三目运算符的区别

     一直都以为这两者之间的三目运算符是没有区别的,只是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执行并不会执行转换代码。

转载于:https://www.cnblogs.com/hrmai/archive/2009/08/05/1539991.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值