C# 枚举中定义的项目总数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/856154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:21:59  来源:igfitidea点击:

Total number of items defined in an enum

c#.netenums

提问by SO User

How can I get the number of items defined in an enum?

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

采纳答案by Kasper Holdum

You can use the static method Enum.GetNameswhich 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

您可以使用静态方法Enum.GetNames,该方法返回一个数组,该数组表示枚举中所有项目的名称。此数组的长度属性等于枚举中定义的项目数

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

回答by Matt Hamilton

Enum.GetValues(typeof(MyEnum)).Length;

Enum.GetValues(typeof(MyEnum)).Length;

回答by Lucas Willett

You can use Enum.GetNames to return an IEnumerable of values in your enum and then .Count the resulting IEnumerable.

您可以使用 Enum.GetNames 返回枚举中的 IEnumerable 值,然后对生成的 IEnumerable 进行计数。

GetNames produces much the same result as GetValues but is faster.

GetNames 产生的结果与 GetValues 大致相同,但速度更快。

回答by jvanderh

From the previous answers just adding code sample.

从之前的答案中,只是添加了代码示例。

 class Program
    {
        static void Main(string[] args)
        {
            int enumlen = Enum.GetNames(typeof(myenum)).Length;
            Console.Write(enumlen);
            Console.Read();
        }
        public enum myenum
        {
            value1,
            value2
        }
    }

回答by Josh

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 来为我们确定这些信息。

回答by Timothy Shields

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

Is the number of items 5? (A, B, C, D, E)

项目数是 5 吗?( A, B, C, D, E)

Or is it 3? (1, 2, 3)

或者是3?( 1, 2, 3)

The number of namesdefined in MyEnum(5) can be computed as follows.

(5) 中定义的名称数量MyEnum可以计算如下。

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

The number of valuesdefined in MyEnum(3) can be computed as follows.

(3) 中定义的的数量MyEnum可以计算如下。

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

回答by S22

For Visual Basic:

对于 Visual Basic:

[Enum].GetNames(typeof(MyEnum)).Lengthdid not work with me, but [Enum].GetNames(GetType(Animal_Type)).lengthdid.

[Enum].GetNames(typeof(MyEnum)).Length没有和我一起工作,但 [Enum].GetNames(GetType(Animal_Type)).length做了。

回答by EnergyWasRaw

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:

我刚才正在研究这个,并且对当前解决方案的可读性不满意。如果您正在非正式地编写代码或在一个小项目中编写代码,您只需在枚举的末尾添加另一个名为“长度”的项目。这样,您只需要键入:

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.

当然,如果其他人要使用您的代码 - 或者我确定在许多其他情况下在这种情况下不适用于我 - 这个解决方案可能从不明智到可怕。

回答by Matt Parkins

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;
}