Search code examples
c#enums

Why is it okay for an enum to have two different names with the same numeric value?


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?


Solution

  • public enum Colour
    {
        Red=10,
        Rouge=10,
        Blue=11,
        Bleu=11,
        Green=12,
        Vert=12,
        Black=13,
        Noir=13
    }