I’m just curious why this code…
enum Tile { Empty, White, Black };
private string TileToString(Tile t)
{
switch (t)
{
case Tile.Empty:
return ".";
case Tile.White:
return "W";
case Tile.Black:
return "B";
}
}
Throws that error. It’s not possible for t to take on any other value, is it? Shouldn’t the compiler be clever enough to figure that out?
No, you can use any
intvalue converted toTile. Try this:An enum is a set of names for numbers, effectively… but neither the compiler nor the CLR enforces that a value of the enum type has a name. It’s a pain, but there it is…
I would suggest a default case which threw
ArgumentException(or possiblyArgumentOutOfRangeException).