Scope of labels in C#



Hi,

In C++, the following code compiles without error:

void main()
{
goto TestLabel;

if (true) {
TestLabel:;
}
}

However, in C# the equivalent code produces an error:

No such label 'TestLabel' within the scope of the goto statement

And a warning:

This label has not been referenced

----

It sounds like the label identifier has a scope that's local to
whatever
set of curly braces it's currently inside, rather than the function
body.
I can understand not letting someone jump in and out of, say, a
"using"
block or a try/catch/finally construct, but this has me baffled. Can
anyone explain to me in language theory terms what's happening here,
or point me to the MSDN pages that describe the scope of labels and
how to create a label with a greater scope that the top of the
compiler's
scope stack? Coming to C# from C++, this has me very confused.

Best regards,

-- Julian Mensch
.



Relevant Pages

  • Re: Scope of labels in C#
    ...   goto TestLabel; ... No such label 'TestLabel' within the scope of the goto statement ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Scope of labels in C#
    ... how to create a label with a greater scope that the top of the compiler's ... ...Each block or switch-block creates a separate declaration ... or if the goto statement is not within the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Infinite Loops and Explicit Exits
    ... > great rush to politically correct code, ... When used with scope ... The problem lies at the label that must exist for it. ... With an END-IF one ...
    (comp.lang.cobol)
  • Re: "We Never Use Assembly Language"
    ... scope it is undefined. ... This is true for ANY label and has nothing ... you seem to feel that the definition of a local symbol ... in assembly programming means a label which scope is limited ...
    (alt.lang.asm)
  • Re: Shapes at Runtime?
    ... Every variable exists within a scope. ... >from the label to the shape. ... >the button click handler and the label click handler, ... >very good approximation) that you want to copy into the form field. ...
    (comp.lang.pascal.delphi.misc)