I have an enum defined as follows:
public enum CrystalTypeEnum { Red, White, Blue, Green };
and I have a static function that returns the string representation of a given enum value:
public static string toString(CrystalTypeEnum type)
{
switch (type)
{
case CrystalTypeEnum.Red:
return "Red";
case CrystalTypeEnum.White:
return "White";
case CrystalTypeEnum.Blue:
return "Blue";
case CrystalTypeEnum.Green:
return "Green";
}
}
When I compile my code I get the following error:
CrystalType.toString(CrystalType.CrystalTypeEnum): not all code paths return a value
Why am I getting this error when clearly my switch statement covers all four cases (Red, White, Blue, Green).
If there is no
default:control is transferred to outside of the switch statement (for values not handled by a case). This means if you don’t have adefault:then you need a return statement after the switch that returns a value of the type defined by the return type of the method.