What determines which name is selected when calling ToString() on an enum value which has multiple corresponding names?

后端 未结 3 748
生来不讨喜
生来不讨喜 2021-02-13 06:10

What determines which name is selected when calling ToString() on an enum value which has multiple corresponding names?

Long explanation

3条回答
  •  悲&欢浪女
    2021-02-13 06:31

    The documentation warns that duplicate values will produce errors.
    You have duplicate values and are getting errors - not a surprise.

    You are getting the first Equal.
    Equal is based solely on value.
    You have no control over the order in which the enum is evaluated.

    As stated in comment an enum expects unique values for the type.

    Enumeration Types (C# Programming Guide)

    However, you should not do this because the implicit expectation is that an enum variable will only hold one of the values defined by the enum. To assign an arbitrary value to a variable of an enumeration type is to introduce a high risk for errors.

    Apparently you have uncovered one of the high risk errors with a duplicate values for type.

    I characterize this error as non deterministic as I consider changing the order but same data the same input given nothing in the spec state of implies enum input is order dependent. OP considers different order as different data. With the OP's assumption would not characterize this a non deterministic. Still fair to characterized it as an error caused by duplicate values for type.

    Another reference that implies uniqueness is expected

    Enum.GetName

    The return is string (not string[]).

    Equals is based solely on value.

    Enum.Equals

    GetName is going to match on the first value.
    That is my definition of non-deterministic.
    How can you know which you have if have if they are considered equal?

    Suspect Microsoft does not enforce uniqueness of enum type values due to overhead.

    OP expects that Enum explicitly associates A value with A string (like a KVP) when there is no indication of that type of association.
    The documentation explicitly warns against making that assumption.
    Where does the documentation indicate Enum is implemented as a set of key value pair, class, or strut?
    The results and methods indicate an Enum is a string and value type (other than char) with a loose association.

    Possible work around.

    A Dictionary does not require unique values.

    public enum RgbColor : byte
    {
        Black,
        Red,
        Green,
        Blue,
        White,
        Default
    }
    
    static void Main(string[] args)
    {
        Dictionary ColorRGB = new Dictionary();
        ColorRGB.Add(RgbColor.Black, 0x000000);
        ColorRGB.Add(RgbColor.Default, 0x0000ff);
        ColorRGB.Add(RgbColor.Blue, 0x0000ff);
        ColorRGB.Add(RgbColor.Green, 0x00ff00);
        ColorRGB.Add(RgbColor.White, 0xffffff);
    
        Debug.WriteLine(ColorRGB[RgbColor.Blue].ToString("X6"));
        Debug.WriteLine(ColorRGB[RgbColor.Default].ToString("X6"));
        Debug.WriteLine(ColorRGB[RgbColor.Black].ToString("X6"));
        Debug.WriteLine(ColorRGB[RgbColor.White].ToString("X6"));
    

提交回复
热议问题