“else if()” versus multiple “if()”s in C#

后端 未结 10 1813
一个人的身影
一个人的身影 2021-01-03 21:51

How do these practically differ?

// Approach one
if (x == 1)
    DoSomething();
else if (x == 2)
    DoSomethingElse();

// Approach two
if (x == 1)
    DoSo         


        
10条回答
  •  花落未央
    2021-01-03 22:43

    When you use multiple else if, it will execute the condition that meets. If there are remaining cases, they will be skipped. When you have multiple if, it will check every statement. So this becomes more of a performance issue.

提交回复
热议问题