I just discovered a subtle bug where I had an enum with two names unintentially sharing the same numeric value (in this case red=10 and crimson=10). I'm a bit surprised this isn't a syntax error.
public enum Colour
{
Red=10,
Blue=11,
Green=12,
Crimson=10
}
// Debug.Write(Colour.Red==Colour.Crimson) outputs True
Is there any real world reason why this behaviour might be a useful or do think it should be a syntax error?
Dictionary<ExternalEnum, TValue>
whereExternalEnum
is in an external assembly I can't modify. The dictionary needed to have a value for every enum value inExternalEnum
, but two values inExternalEnum
have the same numeric value. I then got an ArgumentException because (I think) the dictionary is keyed on the numeric value behind the scenes - I've since had to exclude the duplicated value from the dictionary.