Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I add a goto label at the end of a method?

After researching a way to exit a nested loop, I decided to try using goto,

private void example()
{
    for (int i = 0; i < 100; i++)
    {
        for (int ii = 0; ii < 100; ii++)
        {
            for (int iii = 0; iii < 100; iii++)
            {
                goto exitMethod;
            }                
        }             
    }

exitMethod:
}

But for some reason, if I put a goto label is at the very end of the method, Visual Studio 2012 (Ultimate) complains (and it won't compile),

Screenshot

But if I change my code to this,

private void example()
{
    for (int i = 0; i < 100; i++)
    {
        for (int ii = 0; ii < 100; ii++)
        {
            for (int iii = 0; iii < 100; iii++)
            {
                goto exitMethod;
            }                
        }             
    }

exitMethod:

    int someUnneededVariable; // Just an example, if I add ANY piece of code the error vanishes.
}

None of the errors appear (and it compiles); I've searched through all the MSDN references that I know of, and I couldn't find anything about this.

I know that I could easily solve this problem by using return;; even so, I would still like to find out what's causing this error.

like image 742
Sam Avatar asked Aug 30 '13 14:08

Sam


1 Answers

A label doesn't exist on its own: it labels a statement. From section 8.4 of the C# 5 spec:

A labeled-statement permits a statement to be prefixed by a label. Labeled statements are permitted in blocks, but are not permitted as embedded statements.

In this case, you're applying the label at the end of the method - there's no statement for it to be a label for. So the compiler is absolutely right to reject your code.

If you really wanted to, you could add a label to an otherwise-redundant return statement:

exitMethod:
    return;
}

... or just an empty statement, as suggested by Irfan. There has to be a statement though.

But I wouldn't recommend it. Just change any goto exitMethod; statement to simply return.

like image 75
Jon Skeet Avatar answered Oct 15 '22 21:10

Jon Skeet