c#比较枚举时最好使用开关还是if / else [英] c# is it better to use a switch or if/else when comparing an enum

查看:276
本文介绍了c#比较枚举时最好使用开关还是if / else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有4个值的枚举,我只是好奇使用if / else语句检查值是更好还是使用switch语句。

I have an enum with 4 values and I am just curious as to whether it would be better to use for if/else statements to check what the value is, or use a switch statement.

当我说的更好时,我指的是性能,因为两者彼此可读性强。

When I say better I mean in terms of performance, as both are about as readable as one another.

谢谢

推荐答案

我认为就性能而言,如果只检查4个值,您不会感到任何不同。我发现,与if-else语句相比,switch-case语句产生的IL代码更短。

I think that in terms of performance you won't feel any difference if you have only 4 values to check. I figured out, that switch-case statement produces shorter IL code, than if-else statement.

示例:
此C#代码

Example: This C# Code

static void Main(string[] args)
        {
            var testEnumeration = SomeEnum.Val3;

            if (testEnumeration == SomeEnum.Val1)
                Console.WriteLine("1");
            else if (testEnumeration == SomeEnum.Val2)
                Console.WriteLine("2");
            else if (testEnumeration == SomeEnum.Val3)
                Console.WriteLine("3");
            else if (testEnumeration == SomeEnum.Val4)
                Console.WriteLine("4");

            switch (testEnumeration)
            {
                case SomeEnum.Val1:
                    { Console.WriteLine("1"); break;}
                case SomeEnum.Val2:
                    { Console.WriteLine("2"); break; }
                case SomeEnum.Val3:
                    { Console.WriteLine("3"); break; }
                case SomeEnum.Val4:
                    { Console.WriteLine("4"); break; }
            }

        }

产生以下IL代码:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       180 (0xb4)
  .maxstack  2
  .locals init ([0] valuetype ConsoleTest.Program/SomeEnum testEnumeration,
           [1] bool CS$4$0000,
           [2] valuetype ConsoleTest.Program/SomeEnum CS$4$0001)
  IL_0000:  nop
  IL_0001:  ldc.i4.2
  IL_0002:  stloc.0
  IL_0003:  ldloc.0
  IL_0004:  ldc.i4.0
  IL_0005:  ceq
  IL_0007:  ldc.i4.0
  IL_0008:  ceq
  IL_000a:  stloc.1
  IL_000b:  ldloc.1
  IL_000c:  brtrue.s   IL_001b
  IL_000e:  ldstr      "1"
  IL_0013:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0018:  nop
  IL_0019:  br.s       IL_0061
  IL_001b:  ldloc.0
  IL_001c:  ldc.i4.1
  IL_001d:  ceq
  IL_001f:  ldc.i4.0
  IL_0020:  ceq
  IL_0022:  stloc.1
  IL_0023:  ldloc.1
  IL_0024:  brtrue.s   IL_0033
  IL_0026:  ldstr      "2"
  IL_002b:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0030:  nop
  IL_0031:  br.s       IL_0061
  IL_0033:  ldloc.0
  IL_0034:  ldc.i4.2
  IL_0035:  ceq
  IL_0037:  ldc.i4.0
  IL_0038:  ceq
  IL_003a:  stloc.1
  IL_003b:  ldloc.1
  IL_003c:  brtrue.s   IL_004b
  IL_003e:  ldstr      "3"
  IL_0043:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0048:  nop
  IL_0049:  br.s       IL_0061
  IL_004b:  ldloc.0
  IL_004c:  ldc.i4.3
  IL_004d:  ceq
  IL_004f:  ldc.i4.0
  IL_0050:  ceq
  IL_0052:  stloc.1
  IL_0053:  ldloc.1
  IL_0054:  brtrue.s   IL_0061
  IL_0056:  ldstr      "4"
  IL_005b:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0060:  nop
  IL_0061:  ldloc.0
  IL_0062:  stloc.2
  IL_0063:  ldloc.2
  IL_0064:  switch     ( 
                        IL_007b,
                        IL_0089,
                        IL_0097,
                        IL_00a5)
  IL_0079:  br.s       IL_00b3
  IL_007b:  nop
  IL_007c:  ldstr      "1"
  IL_0081:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0086:  nop
  IL_0087:  br.s       IL_00b3
  IL_0089:  nop
  IL_008a:  ldstr      "2"
  IL_008f:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_0094:  nop
  IL_0095:  br.s       IL_00b3
  IL_0097:  nop
  IL_0098:  ldstr      "3"
  IL_009d:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_00a2:  nop
  IL_00a3:  br.s       IL_00b3
  IL_00a5:  nop
  IL_00a6:  ldstr      "4"
  IL_00ab:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_00b0:  nop
  IL_00b1:  br.s       IL_00b3
  IL_00b3:  ret
} // end of method Program::Main

这篇关于c#比较枚举时最好使用开关还是if / else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆