What is the use of labels in C#?

前端 未结 13 2210
面向向阳花
面向向阳花 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:11

    There is nothing wrong with labels and goto's in themselves. The problem is that people tend to abuse them which does create a problem.

    Typical use of a label

    OperationStart:
      if ( !TrySomeOperation() ) {
        if ( MaybeFixOperation() ) { 
          goto OperationStart;
        }
      }
    

    You'd need to make some assertions that you couldn't hit an infitite loop, but given a reasonable set of guarantees there's nothing inherently wrong with this code.

提交回复
热议问题