tags:

views:

272

answers:

6

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");
+1  A: 

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
+1  A: 

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  A: 

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
+4  A: 

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
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
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
+1  A: 

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-
+1  A: 

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

Usage model: none, unless the alternative is even worse

tvanfosson