Is there any significant difference between using if/else and switch-case in C#?

前端 未结 20 1181
时光取名叫无心
时光取名叫无心 2020-11-22 07:25

What is the benefit/downside to using a switch statement vs. an if/else in C#. I can\'t imagine there being that big of a difference, other than m

20条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 07:38

    One possible downside of switch statements is its lack of multiple conditions. You can have multiple conditions for the if (else) but not multiple cases statements with different conditions in a switch.

    Switch statements are not suitable for logic operations beyond the scope of simple Boolean equations/expressions. For those Boolean equations/expressions, it is eminently suitable but not for other logic operations.

    You have much more freedom with the logic available in If statements but the readability can suffer if the If statement becomes unwieldy or is handled poorly.

    Both have there place depending on the context of what you are faced with.

提交回复
热议问题