What is the use of labels in C#?

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

    Why is it there?

    The reason is actually pretty simple. Assembler and IL don't support advanced instructions like for, do, break, continue and while. Implicitly, code that loops (or more generally: branches) is compiled to branches. goto and label is a very literal branch.

    Note that you simply cannot implement all the contraptions that I can think of easily without a 'goto'- even though there are usually better alternatives (at this point I'd like to point out that there are Design Patterns like the State pattern).

    If you design a language like C#, which is kind-of the "high level assembler for IL", it only makes sense to support at least everything there is in IL. Here, branch / label is the low-level construct, and goto / label is the C# projection (with scope added as high-level construct).

    From the compiler POV

    Eventually if you go down to how a compiler works, there's a difference in predictable code and unpredictable code. The optimizing part of a compiler spends a tremendous amount of effort into normalizing the constructions you're using in your code. Once normalized, it's optimized using patterns.

    Herein lies the problem. If you have a while loop, a for loop or a foreach loop, it will definitely generate a normalized pattern. After all, loops are the number one thing in your code that can be optimized. However, if you're using strange branches, they won't be optimized - simply because it won't be normalized to a standardized pattern and therefore won't be picked up by the pattern matcher.

    This comment is not just about predictability of code patterns - it's also about predictability of data flow. Again, if it's predictable, your compiler can do more than if it's not. In various cases, constructs like break and continue will also lead to more unpredictable branches; labels and goto will just get you there more easily. The result is the same: your performance will suffer if this occurs.

    So while it's true that all compilers change loops into branches of a specific form. The IL compiler is a SSA compiler, just like LLVM. This video of Chandler explains a thing or two about how this works: https://www.youtube.com/watch?v=FnGCDLhaxKU

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-17 21:15

    It's hard to do switch statements without them.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-17 21:18

    I think it was a marketing decision..

    Microsoft wants all kinds of developers using their C# language, and if you add labels, it makes transition for some programmers easier. It also makes it easier to port old code to their language...

    0 讨论(0)
  • 2020-12-17 21:20

    While in principle I believe that there are legitimate uses for the goto statement, in practice I've been developing in C# since it was first released and I didn't know it had a goto statement until now.

    0 讨论(0)
提交回复
热议问题