In C#, in a switch statement non constant values are not allowed in a case label. If you need to support non constant values then you have to use an if statement.
My question is rather simple: Is switch
internally implemented in such a way that restricting to constant values allows some kind of optimization that otherwise couldn't be done? What I'm basically asking is if these two code snippets would perform exactly the same:
switch (myEnumVariable)
{
case MyEnum.Value1:
....
break;
case MyEnum.Value2:
.....
....
default:
....
}
if (myEnumVariable == MyEnum.Type1)
{
....
}
else if (myEnumVariable == MyEnum.Type2)
{
....
}
...
else
{
....
}
Or does the switch statement have an advantage due to it's restriction. If not, why have the restriction?