Possible Duplicate:
Variable declaration in c# switch statement
What is the purpose of the extra braces in Switch case?
Today I was writing a small piece of code and, at debugging time, VS slapped me and told me I made a big mistake with my local variables names: "A local variable named 'i' is already defined in this scope".
MyEnum enumerator = MyEnum.B;
switch (enumerator)
{
case MyEnum.A:
Int32 i = 0;
// DoStuffsA();
break;
case MyEnum.B:
Double i = 0D;
// DoStuffsB();
break;
}
So I said "Ok VS, let me fix it in a moment, I want to scope my cases first as I like them more!". So I made my cosmetic change... but suddenly VS stopped whining about that error:
MyEnum enumerator = MyEnum.B;
switch (enumerator)
{
case MyEnum.A:
{
Int32 i = 0;
// DoStuffsA();
break;
}
case MyEnum.B:
{
Double i = 0D;
// DoStuffsB();
break;
}
}
Ok... I'm aware of the meaning of "scope" and I know I produced two different scopes like so. The problem is that I don't know how switch statements are translated into IL code and I always thought that all the case statements were placed into the switch scope no matter if curly braces were used or not. So... the following oddity can be compiled, right?
MyEnum enumerator = MyEnum.B;
switch (enumerator)
{
case MyEnum.A:
String s = "Hello!";
Console.WriteLine(s);
break;
case MyEnum.B:
s = "Goodbye!";
Console.WriteLine(s);
break;
}
But not the following one:
MyEnum enumerator = MyEnum.B;
switch (enumerator)
{
case MyEnum.A:
{
String s = "hello";
Console.WriteLine(s);
break;
}
case MyEnum.B:
{
s = "goodbye";
Console.WriteLine(s);
break;
}
}
Can some explain me better the behavior of the compiler in this situation? Many thanks!