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

后端 未结 10 2068
一个人的身影
一个人的身影 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:46

    No anwsers about performance?

    So if x=1 then you do only one check in first case, and in second case you do 2 checks, so first case is faster.

提交回复
热议问题