How does CLR determine which Color zero should be converted to?
internal static class Test
{
private static void Main()
{
Console.WriteLine((Color)0);
}
private enum Color
{
Red,
Green = Red
}
}
Using this Color’s definition “Red” will be outputted.
If we use other definitions, results are really very interesting.
private enum Color
{
Red,
Green = Red,
Blue = Red,
Yellow = Red
}
The output is “Green”.
Another definition:
private enum Color
{
Red,
Green = Red,
Blue,
Yellow = Red
}
The output is “Yellow”.
It just returns a
Colorvalue with an underlying value of 0. That’s the same value asColor.RedandColor.Green.Fundamentally, your enum is broken in that
RedandGreenare the same value. You can’t distinguish between them at all.I don’t know if there are any guarantees around whether
ToStringreturns “Red” or “Green” – but if you get to the situation where that’s relevant, you should have described your enum differently.EDIT: From the documentation: