Why (and how) does the order of an Enum influence the ToString value?

前端 未结 2 1186
北恋
北恋 2021-02-06 04:25

I have problems with the "order" of the values of an enum. It\'s a little difficult to explain, that\'s why I wrote up some code:

class Program
{
    pu         


        
2条回答
  •  抹茶落季
    2021-02-06 05:14

    The behaviour is specified to be "undefined" (I thought I'd spotted a pattern just now, but apparently not.) The documentation explicitly calls this out:

    If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return.

    Either make your enum values distinct, or explicitly create a map from value to desired name.

提交回复
热议问题