What is the use of labels in C#?

前端 未结 13 2233
面向向阳花
面向向阳花 2020-12-17 20:18

Labels and GOTOs are considered bad practice and as far as I know there is no reason to use it in C#.

What is the use of labels in C#?

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 21:16

    Labels without goto are useless, they do nothing.

    Using goto is considered a bad practice. But there is a case where it can't be avoided: breaking out of nested loops:

    foreach(...) {
      foreach(...) {
        if(...) {
          goto OuterLabel;
        }
      }
    }
    OuterLabel:
    

    In such a case using the break statement would just break the most inner loop.

提交回复
热议问题