switch statement bug

  • Thread starter Thread starter fred
  • Start date Start date
F

fred

I tested following program gives runtime error:
Unhandled Exception: System.InvalidProgramException:
Common Language Runtime detected an invalid program.

using System;
public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 0:
Console.WriteLine
(0);
goto default;
case 1:
Console.WriteLine
(1);
goto case 0;
default:
Console.WriteLine
("all others");
break;
}
}
}

However, by rearranging the case section, this bug is
avoided:

public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 1:
Console.WriteLine
(1);
goto case 0;
case 0:
Console.WriteLine
(0);
goto default;
default:
Console.WriteLine
("all others");
break;
}
}
}

According to documentation, since C# has a "no fall
through" rule to ensure the order of case doesn't matter,
I think this is a bug.
 
Fall through definitely isn't supported - to prove this, leave a break
statement out of any of the cases and it won't compile.

I think if you switch it to int i = 1 instead of const that you won't have a
problem. The fact that there isn't fall through doesn't necessarily mean
that the ordering doesn't matter. Logically I think that's true, but
architecturally, I'm not sure that's so b/c if you take out the const, you
don't have a problem.
 
fred said:
I tested following program gives runtime error:
Unhandled Exception: System.InvalidProgramException:
Common Language Runtime detected an invalid program.

using System;
public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 0:
Console.WriteLine
(0);
goto default;
case 1:
Console.WriteLine
(1);
goto case 0;
default:
Console.WriteLine
("all others");
break;
}
}
}

Apparently, this bug has been fixed in Framework version 1.1.
 
Back
Top