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

前端 未结 20 832
时光取名叫无心
时光取名叫无心 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:48

    a switch statement basicly is a comparison for equality. keyboard event's have a great advantage over switch statement's when having easy to write and read code then an if elseif statement would, missing a {bracket} could get troubling as well.

    char abc;
    switch(abc)
    {
    case a: break;
    case b: break;
    case c: break;
    case d: break;
    }
    

    An if elseif statement is great for more then one solution if(theAmountOfApples is greater then 5 && theAmountOfApples is less then 10) save your apples else if(theAmountOfApples is greater then 10 || theAmountOfApples == 100) sell your apples. I dont write c# or c++ but I did learn it before I learned java and they are close languages.

提交回复
热议问题