GetName for enum with duplicate values

后端 未结 5 1637
小鲜肉
小鲜肉 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:43

    Experimentation shows that:

    V1 = "value1"

    and

    V2 = "value1"

    However, this isn't guaranteed. The MSDN page on Enum.GetName states:

    If multiple enumeration members have the same underlying value, the GetName method guarantees that it will return the name of one of those enumeration members. However, it does not guarantee that it will always return the name of the same enumeration member. As a result, when multiple enumeration members have the same value, your application code should never depend on the method returning a particular member's name.

提交回复
热议问题