的GetName与重复值枚举 [英] GetName for enum with duplicate values

查看:117
本文介绍了的GetName与重复值枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个C#枚举重复值,称

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 following strings?

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



这是真的,V1和V2必须包含相同的值?
什么这些价值观应该是什么?

Is it true that V1 and V2 must contain the same values? What these values should be?

我还没有发现任何在MSDN或就在这里这么«提领»使用重复枚举,指向我一个链接,请,如果我错过了

I haven't found anything in MSDN or here concerning such a «dereferencing» of enums with duplicates, point me to a link, please, if I missed that.

推荐答案

实验表明:

V1 =值1

V2 =值1

不过,这无法保证。在 Enum.GetName 借助 MSDN页面状态:

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

如果多个枚举成员具有相同的基础值,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.

这篇关于的GetName与重复值枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆