1

Duplicate:

What is the use of labels in c#?

I was trawling through some .NET Source Code using reflector when I came across something which I see very little of on the web, which is the label syntax.

From here I can see that it raises the chance of ugly spaghetti code:

goto 1: 
goto 2: 
if(booleanProperty) goto 1:

Is the purpose of the label simply so you can jump back and fourth inside a function?

What model usages of the C# label would you say there is? Do you have any experience with its implementation yourself? Was it a good experience or not?

if (1 == 1)
    goto Stage2;
Stage1:
    Console.WriteLine("Stage1");
Stage2:
    Console.WriteLine("Stage2");
Community
  • 1
  • 1
REA_ANDREW
  • 10,666
  • 8
  • 48
  • 71
  • Asked just minutes ago: http://stackoverflow.com/questions/662577/what-is-the-use-of-labels-in-c – Dana Mar 19 '09 at 15:53
  • Lol, there must be some common topic myself and the other author of that question are on - it is ironic that I stated I see very little of it on the web, and now there are two questions regarding in with in a hour of each other. Crazy. – REA_ANDREW Mar 19 '09 at 15:57
  • Probably a report assignment on why goto is bad? – dance2die Mar 19 '09 at 15:58

6 Answers6

4

You see this in reflector most often because the programmer used a higher-level construct that was re-written behind the scenes so we now see goto/labels instead.

When considering whether to use a label as a programmer, my rule of thumb is that if you have to ask you probably shouldn't do it.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • It's not that the compiler re-wrote it to use gotos. Gotos and loops all compile to the same branching instructions. It's whether or not the decompiler can figure it all out that determines whether you see the original construct or something different in Reflector. – P Daddy Mar 19 '09 at 15:59
  • e.g., if I write "int i = 0; \n StartLoop: \n if(i >= 100) goto EndLoop; \n Console.WriteLine(i); \n i = i + 1; \n goto StartLoop; \n EndLoop:", compile and Reflector it, I see "for(int i = 0; i < 100; i++) Console.WriteLine(i);" – P Daddy Mar 19 '09 at 16:10
2

There are a very few situations - usually within autogenerated code representing a state machine - where goto can make life simpler. There's usually a way round it if you think hard enough, but for autogenerated code it's easier to work out a simple but less readable way of doing it than try to generate the code you'd write by hand.

Can't say I've used it myself in C#, but IIRC I did something similar when porting the Colossal Cave adventure to Java.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Goto is highly controversial. However, the code you encountered was probably refactored from a more readable c# source code into labels and branches which more closely aligns with the IL structures.

hova
  • 2,811
  • 20
  • 19
1

The only time I've found the label/goto syntax useful in C# is when implementing fall-through in case statements. This is something that I found occasionally useful in Java that C# doesn't allow. Otherwise I avoid labels and goto like the plague.

dustyburwell
  • 5,755
  • 2
  • 27
  • 34
1

there are various usages of goto, taking into account, always avoid spaghetti code, and all the advice in previous responses:

    while (loop1) {
    while (loop2) {
        if (timeToQuit) 
            break outerLabel;
    }
}
outerLabel:

In a Switch

switch(value) {
    case 1:
        // DoSomething();
        break;
    case 2:
        // DoSomethingElse();
        break;
    default:
        goto case 1;
}
Jhonny D. Cano -Leftware-
  • 17,663
  • 14
  • 81
  • 103
1

Benefit: job security? -- because it can really make your code unreadable to anyone else

Usage model: none, unless the alternative is even worse

tvanfosson
  • 524,688
  • 99
  • 697
  • 795