I have the following code that is unreachable and i do not understand why:
Language CheckLanguage (int theLanguage) {
switch (theLanguage) {
case 0:
return Language.English;
break; << warning CS0162: Unreachable code detected
case 1:
return Language.Spanish;
break;
}
return Language.Spanish;
}
Also, as i need a return in the bottom, tried without, and “null” does not work i have constructed it this way and it works. However, i guess this is part of the problem but what would be a better way of designing this piece of code?
The break is unreachable as the function will return before it gets to the break. However you can’t build a switch without the break statement. (I think)
That’s why its a warning, not an error. This code will still function.
But if it bothers you try this:
Language CheckLanguage (int theLanguage) {
Language returnValue = Language.Spanish
switch (theLanguage) {
case 0:
returnValue = Language.English;
break;
case 1:
returnValue = Language.Spanish;
break;
}
return returnValue;
}