Jak zapętlić wszystkie wartości enum w C#? [duplikat]

To pytanie ma już odpowiedź tutaj:
jak wyliczyć enum w C#? 26 odpowiedzi

public enum Foos
{
    A,
    B,
    C
}

Czy istnieje sposób na przepętanie możliwych wartości Foos?

W zasadzie?
foreach(Foo in Foos)
Author: Cœur, 2009-06-10

8 answers

Tak możesz użyć metody GetValue‍‍‍s:

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

Lub wersja na literę:

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

Dawno temu dodałem funkcję helpera do mojej prywatnej biblioteki na taką właśnie okazję:

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

Użycie:

var values = EnumUtil.GetValues<Foos>();
 1633
Author: JaredPar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-28 13:34:05
foreach(Foos foo in Enum.GetValues(typeof(Foos)))
 686
Author: SLaks,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-01 11:27:34
foreach (EMyEnum val in Enum.GetValues(typeof(EMyEnum)))
{
   Console.WriteLine(val);
}

Kredyt dla Jona Skeeta tutaj: http://bytes.com/groups/net-c/266447-how-loop-each-items-enum

 99
Author: Inisheer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-06-09 20:28:56
foreach (Foos foo in Enum.GetValues(typeof(Foos)))
{
    ...
}
 48
Author: adrianbanks,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-06-09 20:43:08

Aktualizacja
Jakiś czas później widzę komentarz, który przywraca mi dawną odpowiedź i myślę, że teraz zrobiłbym to inaczej. W dzisiejszych czasach napisałbym:

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>();
}
 28
Author: Neil Barnwell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-04 10:48:55
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
}
 20
Author: dbones,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-08 15:01:37
 Enum.GetValues(typeof(Foos))
 7
Author: Vasu Balakrishnan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-06-09 20:30:07

Tak. Użycie GetValues() metoda w System.Enum klasy.

 6
Author: Pablo Santa Cruz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-09 21:24:00