在c#中使用switch语句和if/else语句的优缺点是什么?除了代码的外观,我无法想象有这么大的区别。

是否有任何原因导致最终的IL或相关的运行时性能会有根本的不同?

相关:什么是更快,开关上字符串或elseif上类型?


当前回答

如果你只使用If或else语句基解使用比较?操作符

(value == value1) ? (type1)do this : (type1)or do this;

你可以在开关中执行或程序

switch(typeCode)
{
   case TypeCode:Int32:
   case TypeCode.Int64:
     //dosomething here
     break;
   default: return;
}

其他回答

有三个理由支持这种转变:

A compiler targeting native code can often compile a switch statement into one conditional branch plus an indirect jump whereas a sequence of ifs requires a sequence of conditional branches. Depending on the density of cases a great many learned papers have been written about how to compile case statements efficiently; some are linked from the lcc compiler page. (Lcc had one of the more innovative compilers for switches.) A switch statement is a choice among mutually exclusive alternatives and the switch syntax makes this control flow more transparent to the programmer then a nest of if-then-else statements. In some languages, including definitely ML and Haskell, the compiler checks to see if you have left out any cases. I view this feature as one of the major advantages of ML and Haskell. I don't know if C# can do this.

一个趣闻:在托尼·霍尔接受终身成就奖的演讲中,我听到他说,在他职业生涯中所做的所有事情中,有三件事是他最自豪的:

发明快速排序 发明switch语句(Tony称之为case语句) 开始和结束他的工业生涯

我无法想象没有开关的生活。

一般来说(考虑到所有语言和所有编译器),switch语句有时可能比if / else语句更有效,因为编译器很容易从switch语句生成跳转表。如果有适当的约束,也可以对if / else语句做同样的事情,但这要困难得多。

在c#的情况下,这也是正确的,但出于其他原因。

对于大量字符串,使用switch语句具有显著的性能优势,因为编译器将使用哈希表来实现跳转。

使用少量的字符串,两者之间的性能是相同的。

这是因为在这种情况下,c#编译器不会生成跳转表。相反,它生成等效于IF / ELSE块的MSIL。

有一个“switch statement”MSIL指令,当被触发时将使用一个跳转表来实现一个switch语句。但是,它只适用于整数类型(这个问题询问的是字符串)。

对于少量的字符串,编译器生成IF / ELSE块比使用哈希表更有效。

当我最初注意到这一点时,我做了一个假设,因为IF / ELSE块用于少量字符串,编译器对大量字符串做了相同的转换。

这是错误的。“IMA”好心地向我指出了这一点(嗯……他不怎么客气,但他是对的,我错了,这是最重要的部分)

我还对MSIL中缺少“switch”指令做了一个愚蠢的假设(我想,如果有一个switch原语,为什么他们不把它与哈希表一起使用,所以一定没有一个switch原语....). 这是错误的,而且我非常愚蠢。IMA再次向我指出了这一点。

我在这里更新是因为这是评分最高的帖子,也是公认的答案。

然而,我把它变成了社区维基,因为我认为我不应该因为犯错而获得REP。如果你有机会,请给ima的帖子投票。

我刚刚注意到的是,你可以结合if/else和切换语句!在需要检查先决条件时非常有用。

if (string.IsNullOrEmpty(line))
{
    //skip empty lines
}
else switch (line.Substring(0,1))
{
    case "1":
        Console.WriteLine(line);
        break;
    case "9":
        Console.WriteLine(line);
        break;
    default:
        break;
}

switch语句肯定比if else if语句快。有速度测试,已经提供了对它的黑黄蜂

http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx

——看看吧

但很大程度上取决于你要考虑的可能性,但我尽量使用switch语句。

如果你只使用If或else语句基解使用比较?操作符

(value == value1) ? (type1)do this : (type1)or do this;

你可以在开关中执行或程序

switch(typeCode)
{
   case TypeCode:Int32:
   case TypeCode.Int64:
     //dosomething here
     break;
   default: return;
}