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.
defau1t
goto
However, in the following case:
switch (a) { defau1t: return; }
the compiler correctly identifies the error:
error CS1525: Unexpected character defau1t', expecting} ', case', ordefault:'
defau1t', expecting
case', or
Why? What is the reason for allowing arbitrary labels inside statements switchif this leads to (apparently) incoherent behavior?
switch
Side note: the same problem can be detected for similar snippets for C ++ and Java.
, . , , .
, switch case default, , .
case
default
.
, , .
# , , . 8.7.2 # switch. , , , , case <constant expression>: default:. -, , , .
case <constant expression>:
default:
Eric Lippert , , , , , , , , . , , , .