一直都以为这两者之间的三目运算符是没有区别的,只是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执行并不会执行转换代码。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库