I have two functions, one can be compiled and the other cannot. What is the difference?
Does function number 1 assume that case 1 always will be hit, or it just a compiler problem?
public void Test(int x)
{
switch (x)
{
case 1:
uint cId = (uint)3;
break;
case 2:
cId = (uint)5; //NO ERROR HERE. WHY?
break;
}
}
public void DeclaringInsideSwitch(int x)
{
uint tst = 0;
switch (x)
{
case 1:
int y = 3;
uint variable = tst;
break;
case 2:
variable++; //ERROR HERE. WHY?
break;
}
}
I tried of course searching for "Declaring variables inside switch case in C#", but to me it just seems like some sort of bug in C# now, preserved for backward compatibility.
// After getting a warning that it was already answered, my question can now be reduced to what it is really about.
Why:
int x;
x++;
doesn't this work?