What is the use of labels in C#?

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

    When you are implementing a small finite state machine you can use a label for each state and goto for state transitions. This is one of the standard methods of implanting finite state machines and can lead to clear code, provided there is a diagram of the state machine in a document that the code comments point to.

    Sometimes the problem domain contains lots of state machiens, (e.g. telecoms protocols are often defined by finite state machines), most of the time you don’t see finite state machine often.

    Gotos and labels are also very useful for machine-generated code, if you are writing a simple compiler that outputs C# you may be very glad of them.

提交回复
热议问题