枚举中定义的项目总数

本文翻译自:Total number of items defined in an enum

如何获取枚举中定义的项目数?


#1楼

参考:https://stackoom.com/question/3aiw/枚举中定义的项目总数


#2楼

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: 我在C回答这个问题时看到的一个漂亮的技巧,只需在枚举中添加最后一个元素并使用它来说明枚举中有多少元素:

enum MyType {
  Type1,
  Type2,
  Type3,
  NumberOfTypes
}

In the case where you're defining a start value other than 0, you can use NumberOfTypes - Type1 to ascertain the number of elements. 在您定义0以外的起始值的情况下,可以使用NumberOfTypes - Type1来确定元素的数量。

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更快,而且我也不确定它是否被认为是正确的方法,因为我们让Enum为我们确定这些信息。


#3楼

The question is: 问题是:

How can I get the number of items defined in an enum? 如何获取枚举中定义的项目数?

The number of "items" could really mean two completely different things. “项目”的数量可能真的意味着两件完全不同的东西。 Consider the following example. 请考虑以下示例。

enum MyEnum
{
    A = 1,
    B = 2,
    C = 1,
    D = 3,
    E = 2
}

What is the number of "items" defined in MyEnum ? MyEnum定义的“项目”数量是MyEnum

Is the number of items 5? 物品数量是5? ( A , B , C , D , E ) ABCDE

Or is it 3? 还是3? ( 1 , 2 , 3 ) 123

The number of names defined in MyEnum (5) can be computed as follows. MyEnum (5)中定义的名称数量可以如下计算。

var namesCount = Enum.GetNames(typeof(MyEnum)).Length;

The number of values defined in MyEnum (3) can be computed as follows. MyEnum (3)中定义的的数量可以如下计算。

var valuesCount = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().Distinct().Count();

#4楼

For Visual Basic: 对于Visual Basic:

[Enum].GetNames(typeof(MyEnum)).Length did not work with me, but [Enum].GetNames(GetType(Animal_Type)).length did. [Enum].GetNames(typeof(MyEnum)).Length不适用于我,但[Enum].GetNames(GetType(Animal_Type)).length确实如此。


#5楼

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". 如果您正在非正式地编写代码或在小项目上编写代码,则可以在枚举的末尾添加另一个名为“Length”的项。 This way, you only need to type: 这样,您只需输入:

var namesCount = (int)MyEnum.Length;

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. 当然,如果其他人打算使用你的代码 - 或者我确信在这种情况下不适用于我的许多其他情况 - 这个解决方案可能是从不明智到可怕的任何地方。


#6楼

If you find yourself writing the above solution as often as I do then you could implement it as a generic: 如果您发现自己经常编写上述解决方案,那么您可以将其作为通用实现:

public static int GetEnumEntries<T>() where T : struct, IConvertible 
{
    if (!typeof(T).IsEnum)
        throw new ArgumentException("T must be an enumerated type");

    return Enum.GetNames(typeof(T)).Length;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值