Scope of labels in C#

J

Julian Mensch

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
 
I

Ignacio Machin ( .NET/ C# MVP )

  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

Do not use labels at all :)
 
J

Julian Mensch

Does that help answer your question?

Yes. Thank you very much for the prompt and detailed reply.

It still seems incredibly _strange_, for a language based on
C/C++, but it may be the Java influence showing there.

-- Julian Mensch
 
B

Bill Butler

Julian Mensch said:
Yes. Thank you very much for the prompt and detailed reply.

It still seems incredibly _strange_, for a language based on
C/C++, but it may be the Java influence showing there.

I am curious.
Is this purely an academic question or were you really wanting to write code
like your sample.
goto TestLabel;

if (true) {
TestLabel:;

If this is of academic interest, OK.

If you intend to produce real code using constructs like this, I would
really like to talk you down off of the ledge.

Soooo.

Why do you need this functionality?

Bill
 
P

Pavel Minaev

But in any case, even if C# were based on C/C++, there are lots of  
examples of things that work in C/C++ and which don't work in C#.

Speaking of labels and goto - depending on how you look at those
things, it may be either a blessing or a disappointment that Duff's
Device cannot be written in C# ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top