性能问题:if(isChecked)与if(isChecked == true)

Moh*_*deh -1 c# performance if-statement runtime compile-time

CompiletimeRuntime中使用if (isChecked)vs. 有任何性能问题吗?if (isChecked == true)

Ond*_*cek 8

没有任何性能问题.为这两种情况生成的IL完全相同,当IL相同时,它的执行将是相同的.所以没有运行时差异.

bool x = true;
if (x == true) // or (x)
    Console.WriteLine("True");

IL_0001:  ldc.i4.1    
IL_0002:  stloc.0     // x
IL_0003:  ldloc.0     // x
IL_0004:  ldc.i4.0    
IL_0005:  ceq         
IL_0007:  stloc.1     // CS$4$0000
IL_0008:  ldloc.1     // CS$4$0000
IL_0009:  brtrue.s    IL_0016
IL_000B:  ldstr       "True"
IL_0010:  call        System.Console.WriteLine
Run Code Online (Sandbox Code Playgroud)

安装LINQPad并在下次自己尝试;)

至于编译时,如评论中所述,生成的抽象语法树实际上会有所不同.这是AST的相关部分if(x)

在此输入图像描述

而现在 if(x == true)

在此输入图像描述

你可以看到差异.