Here is the class (relax, it’s just an example and not intended for concrete use):
public class Sample
{
public Sample()
{
}
public string Size
{
get
{
switch (index)
{
case 0: return 100;
break;
case 1: return 500;
break;
case 2: return 1000;
break;
...
}
}
}
}
I am getting an “Unreachable code detected” error on the break keyword. Is it even possible to write switch statements inside a class or class member function? Or, am I stuck with if-else blocks?
Based on the feedback below, I see I made a silly error, with return preceding the break
You are getting a unreachable code on the
breakbecause the code is unreachable. Thereturnensures that. You can just remove thebreakstatements and everything should work fine.You can rewrite the switch in either of these two ways:
or