加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:10:09 所属栏目:百科 来源:网络整理
导读:如果我在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或这里有
如果我在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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读