c# – 具有重复值的枚举的GetName

前端之家收集整理的这篇文章主要介绍了c# – 具有重复值的枚举的GetName前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我在C#枚举中有重复的值,说
enum MyE {
  value1 = 1,value2 = 2,valued = 1
}

以下字符串的值应该是什么?

MyE N = (MyE)1;
string V1 = N.ToString();
string V2 = GetName(MyE,1);

V1和V2是否必须包含相同的值?
这些值应该是什么?

我没有在MSDN或这里有关于这样一个“重新引用枚举”的引用,请指出一个链接,如果我错过了.

解决方法

实验表明:

V1 = “value1”

V2 = “value1”

但是,这并不能保证. MSDN page on Enum.GetName说:

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.

猜你在找的C#相关文章