Since your indentation is misleading, the first code is actually:
var s = "Nice"; switch (s) { case "HI": break; const string x = "Nice"; case x: Console.Write("Y"); break; }
That is, x declared inside the case (although after break ), where it is valid. However, directly inside the switch its value is invalid - the only valid statements are case and default .
In addition, const declarations are evaluated at compile time, so x is determined even if the sentence is a break .
However, note that the Mono C # compiler does not compile this code, it complains that the "name" x does not exist in the current scope ", so Mono seems to perform more checks than the .NET compiler. However, I cannot find any or rules in the C # standard that prohibit this use of the const declaration, so I assume that the .NET compiler is right and the Mono compiler is wrong.
Konrad Rudolph May 22 '13 at 14:18 2013-05-22 14:18
source share