I have an enumeration and a switch statement based on this enum, which looks like this:
public enum MyEnum
{
VAL1,
VAL2,
VAL3,
ONE = 1,
TWO = 2
}
and switch:
switch ((MyEnum)Enum.Parse(typeof(MyEnum), input.ToUpper()))
{
case MyEnum.VAL1:
Console.WriteLine("val1");
break;
case MyEnum.VAL2:
Console.WriteLine("val2");
break;
case MyEnum.VAL3:
Console.WriteLine("val3");
break;
case MyEnum.ONE:
Console.WriteLine("1");
break;
default:
Console.WriteLine("default");
break;
}
where input is a string. The problem with me is that I have a compiler error,
The label "case 1:" already appears in the switch statement
I found that moving the "ONE" element as the first in the enumeration resolves the problem, but my question is: why is this happening?
source
share