What is the reason for allowing arbitrary labels inside switch statements?

The following code compiles:

int a = 0;

switch (a)
{
    case 1:
        return;
    defau1t:      // note the typo
        return;
}

because it is defau1tinterpreted as a label goto.

However, in the following case:

switch (a)
{
    defau1t:
        return;
}

the compiler correctly identifies the error:

error CS1525: Unexpected character defau1t', expecting} ', case', ordefault:'

Why? What is the reason for allowing arbitrary labels inside statements switchif this leads to (apparently) incoherent behavior?

Side note: the same problem can be detected for similar snippets for C ++ and Java.

+4
source share
2 answers

, . , , .

, switch case default, , .

.

, , .

+9

# , , . 8.7.2 # switch. , , , , case <constant expression>: default:. -, , , .

Eric Lippert , , , , , , , , . , , , .

+1

All Articles