How can a case in a switch not have a break?

This question already has an answer here:

  • Switch statement fallthrough in C#? 14 answers

  • This structure:

    case "LogOnManager":
    case "NormalUse":
        lblSwipeStatus.Text = "Pass ID BadgenNear The Reader";
        break;
    

    basically means that the code in the second case will be used for either case . So both of the cases use the same code.

    Semantically this could be thought of as:

    case "LogOnManager" || "NormalUse":
        lblSwipeStatus.Text = "Pass ID BadgenNear The Reader";
        break;
    

    Except that this doesn't compile as a valid condition for that switch statement. (Potentially for a couple of reasons if you want to get really technical, but primarily because this evaluates to a bool and the switch is operating on a string .) So the version you've found does the job instead.

    When a break statement is omitted (or, more specifically, when a case block is empty), the process will continue to whatever the next case is, regardless of the value used for that next case .


    If a switch case doesn't have a break, then it will fall through the next case. Note that in C#, it only works if there is nothing in the case body.

    From the Documentation:

    C# does not support an implicit fall through from one case label to another. The one exception is if a case statement has no code.


    A case without a break in this case just means that it will do the same as the case underneath it.

    So either of these cases will do the same thing:

      case "LogOnManager":
      case "NormalUse":
                        lblSwipeStatus.Text = "Pass ID BadgenNear The Reader";
                        break;
    

    It's called fall through

    链接地址: http://www.djcxy.com/p/84478.html

    上一篇: C#:非法交换声明的解决方法?

    下一篇: 交换机中的情况如何不会中断?