你能循环遍历所有枚举值吗?(复制)

时间:2022-02-28 22:47:11

This question already has an answer here:
How to enumerate an enum? 14 answers

这个问题已经有了答案:如何枚举枚举枚举?14个答案

public enum Foos
{
    A,
    B,
    C
}

Is there a way to loop through the possible values of Foos?

有没有办法循环遍历Foos的可能值?

Basically?

基本上吗?

foreach(Foo in Foos)

8 个解决方案

#1


1571  

Yes you can use the ‍GetValue‍‍‍s method:

是的,你可以使用GetValue‍‍‍‍年代方法:

var values = Enum.GetValues(typeof(Foos));

Or the typed version:

或输入版本:

var values = Enum.GetValues(typeof(Foos)).Cast<Foos>();

I long ago added a helper function to my private library for just such an occasion:

我很久以前就在我的私人图书馆里添加了一个辅助功能:

public static class EnumUtil {
    public static IEnumerable<T> GetValues<T>() {
        return Enum.GetValues(typeof(T)).Cast<T>();
    }
}

Usage:

用法:

var values = EnumUtil.GetValues<Foos>();

#2


658  

foreach(Foos foo in Enum.GetValues(typeof(Foos)))

#3


93  

foreach (EMyEnum val in Enum.GetValues(typeof(EMyEnum)))
{
   Console.WriteLine(val);
}

Credit to Jon Skeet here: http://bytes.com/groups/net-c/266447-how-loop-each-items-enum

Jon Skeet报道:http://bytes.com/groups/net-c/266447-how-loop-each-items-enum

#4


45  

foreach (Foos foo in Enum.GetValues(typeof(Foos)))
{
    ...
}

#5


26  

UPDATED
Some time on, I see a comment that brings me back to my old answer, and I think I'd do it differently now. These days I'd write:

更新了一段时间后,我看到了一条评论,让我想起了我以前的答案,我想我现在应该换一种方式。这些天我会写:

private static IEnumerable<T> GetEnumValues<T>()
{
    // Can't use type constraints on value types, so have to do check like this
    if (typeof(T).BaseType != typeof(Enum))
    {
        throw new ArgumentException("T must be of type System.Enum");
    }

    return Enum.GetValues(typeof(T)).Cast<T>();
}

#6


20  

static void Main(string[] args)
{
    foreach (int value in Enum.GetValues(typeof(DaysOfWeek)))
    {
        Console.WriteLine(((DaysOfWeek)value).ToString());
    }

    foreach (string value in Enum.GetNames(typeof(DaysOfWeek)))
    {
        Console.WriteLine(value);
    }
    Console.ReadLine();
}

public enum DaysOfWeek
{
    monday,
    tuesday,
    wednesday
}

#7


6  

 Enum.GetValues(typeof(Foos))

#8


5  

Yes. Use GetValues() method in System.Enum class.

是的。在系统中使用GetValues()方法。Enum类。

#1


1571  

Yes you can use the ‍GetValue‍‍‍s method:

是的,你可以使用GetValue‍‍‍‍年代方法:

var values = Enum.GetValues(typeof(Foos));

Or the typed version:

或输入版本:

var values = Enum.GetValues(typeof(Foos)).Cast<Foos>();

I long ago added a helper function to my private library for just such an occasion:

我很久以前就在我的私人图书馆里添加了一个辅助功能:

public static class EnumUtil {
    public static IEnumerable<T> GetValues<T>() {
        return Enum.GetValues(typeof(T)).Cast<T>();
    }
}

Usage:

用法:

var values = EnumUtil.GetValues<Foos>();

#2


658  

foreach(Foos foo in Enum.GetValues(typeof(Foos)))

#3


93  

foreach (EMyEnum val in Enum.GetValues(typeof(EMyEnum)))
{
   Console.WriteLine(val);
}

Credit to Jon Skeet here: http://bytes.com/groups/net-c/266447-how-loop-each-items-enum

Jon Skeet报道:http://bytes.com/groups/net-c/266447-how-loop-each-items-enum

#4


45  

foreach (Foos foo in Enum.GetValues(typeof(Foos)))
{
    ...
}

#5


26  

UPDATED
Some time on, I see a comment that brings me back to my old answer, and I think I'd do it differently now. These days I'd write:

更新了一段时间后,我看到了一条评论,让我想起了我以前的答案,我想我现在应该换一种方式。这些天我会写:

private static IEnumerable<T> GetEnumValues<T>()
{
    // Can't use type constraints on value types, so have to do check like this
    if (typeof(T).BaseType != typeof(Enum))
    {
        throw new ArgumentException("T must be of type System.Enum");
    }

    return Enum.GetValues(typeof(T)).Cast<T>();
}

#6


20  

static void Main(string[] args)
{
    foreach (int value in Enum.GetValues(typeof(DaysOfWeek)))
    {
        Console.WriteLine(((DaysOfWeek)value).ToString());
    }

    foreach (string value in Enum.GetNames(typeof(DaysOfWeek)))
    {
        Console.WriteLine(value);
    }
    Console.ReadLine();
}

public enum DaysOfWeek
{
    monday,
    tuesday,
    wednesday
}

#7


6  

 Enum.GetValues(typeof(Foos))

#8


5  

Yes. Use GetValues() method in System.Enum class.

是的。在系统中使用GetValues()方法。Enum类。