Someone asked me today if there was a way to test the default statement in this piece of code:
public enum AEnum
{
One,
Two,
Three
}
public void DoStuff(AEnum e)
{
switch (e)
{
case AEnum.One:
Console.WriteLine("AEnum.One");
break;
case AEnum.Two:
Console.WriteLine("AEnum.Two");
break;
case AEnum.Three:
Console.WriteLine("AEnum.Three");
break;
default:
throw new Exception("Unhandled enumeration value");
break;
}
}
Unfortunately I don't see any way to do it without changing the design to something very wrong and ugly like:
public void DoStuff(Enum e)
{
AEnum ae = (AEnum)e;
switch (ae)
{
case AEnum.One:
Console.WriteLine("AEnum.One");
break;
case AEnum.Two:
Console.WriteLine("AEnum.Two");
break;
case AEnum.Three:
Console.WriteLine("AEnum.Three");
break;
default:
throw new Exception("Unhandled enumeration value");
break;
}
}