GetName for enum with duplicate values

后端 未结 5 1652
小鲜肉
小鲜肉 2020-12-07 00:46

If I have duplicate values in a C# enum, saying

enum MyE {
  value1 = 1,
  value2 = 2,
  valued = 1
}

What should be the values of the foll

5条回答
  •  天涯浪人
    2020-12-07 01:44

    Whilst this might be a little late to the Party, this may not be the greatest idea, however, the one work around I did find in my case in which I've had to use duplicate enums is to use the following from the question here; Get String Name from Enum in C#

    var name = nameof(DeclaredEnum.EnumName);
    

    Resulting in:

    EnumName
    

    You can read more Here as to how it actually works.

提交回复
热议问题