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

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

Beware! If your enum has multiple elements with the same value, you may get unexpected results when you use Enum.Parse(). Doing so will arbitrarily return the first element that has the requested value. For example, if you have enum Car { Ford = 1, Chevy = 1, Mazda = 1}, then (Car)Enum.Parse(typeof(Car), "1") will return Car.Ford. While that might be useful (I'm not sure why it would be), in most situations it's probably going to be confusing (especially for engineers maintaining the code) or easily overlooked when problems arise.


I have seen that this feature is sometimes used for a "default" value:

public enum Scope
{
    Transient,
    Singleton,
    Default=Transient
}

But pay attention, this is only sugar for the user of your enum. Just because it is called Default it does not mean that it is the initial value.

Tags:

C#

Enums