枚举中定义的项目总数
如何获取枚举中定义的项目数?
How can I get the number of items defined in an enum?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何获取枚举中定义的项目数?
How can I get the number of items defined in an enum?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(12)
您可以使用静态方法
Enum.GetNames
< /a> 返回一个数组,表示枚举中所有项目的名称。 该数组的 length 属性等于枚举中定义的项目数You can use the static method
Enum.GetNames
which returns an array representing the names of all the items in the enum. The length property of this array equals the number of items defined in the enum问题是:
“物品”的数量实际上可能意味着两种完全不同的事物。 考虑以下示例。
MyEnum
中定义的“项目”数量是多少?物品数量是 5 吗? (
A
,B
,C
,D
,E
)还是 3 ? (
1
,2
,3
)MyEnum
中定义的名称数量 ( 5) 可按下式计算。MyEnum
(3) 中定义的值数量可以按如下方式计算。The question is:
The number of "items" could really mean two completely different things. Consider the following example.
What is the number of "items" defined in
MyEnum
?Is the number of items 5? (
A
,B
,C
,D
,E
)Or is it 3? (
1
,2
,3
)The number of names defined in
MyEnum
(5) can be computed as follows.The number of values defined in
MyEnum
(3) can be computed as follows.Enum.GetValues(typeof(MyEnum)).Length ;
Enum.GetValues(typeof(MyEnum)).Length;
我今天运行了一个基准测试并得出了有趣的结果。 在这三个中:
GetNames(enum) 是迄今为止最快的!
I've run a benchmark today and came up with interesting result. Among these three:
GetNames(enum) is by far the fastest!
我在这个问题的 C 答案中看到了一个巧妙的技巧,只需将最后一个元素添加到枚举中,并用它来告诉枚举中有多少个元素:
在定义除 0 以外的起始值的情况下,您可以使用 NumberOfTypes - Type1 来确定元素的数量。
我不确定这种方法是否比使用 Enum 更快,而且我也不确定它是否被认为是执行此操作的正确方法,因为我们有 Enum 来为我们确定此信息。
A nifty trick I saw in a C answer to this question, just add a last element to the enum and use it to tell how many elements are in the enum:
In the case where you're defining a start value other than 0, you can use NumberOfTypes - Type1 to ascertain the number of elements.
I'm unsure if this method would be faster than using Enum, and I'm also not sure if it would be considered the proper way to do this, since we have Enum to ascertain this information for us.
您可以使用
Enum.GetNames
返回枚举中的IEnumerable
值,然后。对结果 IEnumerable 进行计数
。GetNames
生成的结果与GetValues
大致相同,但速度更快。You can use
Enum.GetNames
to return anIEnumerable
of values in your enum and then.Count
the resulting IEnumerable.GetNames
produces much the same result asGetValues
but is faster.从前面的答案中仅添加代码示例。
From the previous answers just adding code sample.
如果您发现自己像我一样经常编写上述解决方案,那么您可以将其实现为通用的:
If you find yourself writing the above solution as often as I do then you could implement it as a generic:
对于 Visual Basic:
[Enum].GetNames(typeof(MyEnum)).Length
不适用于我,但是[Enum].GetNames(GetType(Animal_Type)).length
做到了。For Visual Basic:
[Enum].GetNames(typeof(MyEnum)).Length
did not work with me, but[Enum].GetNames(GetType(Animal_Type)).length
did.我刚刚正在研究这个问题,并对当前解决方案的可读性不满意。 如果您正在非正式地或在小型项目中编写代码,则只需在枚举末尾添加另一个名为“Length”的项目即可。 这样,您只需要输入:
当然,如果其他人要使用您的代码 - 或者我确信在许多其他情况下,在这种情况下不适用于我 - 这个解决方案可能是从不明智到糟糕的任何地方。
I was looking into this just now, and wasn't happy with the readability of the current solution. If you're writing code informally or on a small project, you can just add another item to the end of your enum called "Length". This way, you only need to type:
Of course if others are going to use your code - or I'm sure under many other circumstances that didn't apply to me in this case - this solution may be anywhere from ill advised to terrible.
对于通用
GetNames()
重载,有一种更简洁的方法:Enum.GetNames<>() 方法
There is a more concise way with the generic
GetNames()
overload:Enum.GetNames<>() method
如果只是为了知道 ENUM 里面有多少项。 我将这样做:
最后一个 ENUM 项目
MyEnum.TOTAL_ENUM_ITEM
将向您显示 ENUM 内的项目数。这并不花哨,但很有效:)
If it only to know how many items inside the ENUM. I will do this instead:
The last ENUM item
MyEnum.TOTAL_ENUM_ITEM
will show you the number of items inside the ENUM.It's not fancy, but it works :)