이넘 값을 루프 모든 방법을 통해 C # 에서?

&gt. 이 질문에 답을 이미 here: &gt. 26 대답 https://stackoverflow.com/questions/105372/how-to-enumerate-an-enum

텍스트 - 끝 - &lt 자동으로 삽입됨 >;!

public enum Foos
{
    A,
    B,
    C
}

반복할 수 있는 방법이 푸 값을 통해 '?'

기본적으로?

foreach(Foo in Foos)
해결책

's' 제바루이 방법을 사용할 수 있습니다.

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

또는 입력되었는지 버전:

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

오래 전에 내가 그냥 내 전용 library) 을 계기로 같은 보조 기능을 추가했다.

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

사용법:

var values = EnumUtil.GetValues();
해설 (12)
foreach(Foos foo in Enum.GetValues(typeof(Foos)))
해설 (4)
foreach (EMyEnum val in Enum.GetValues(typeof(EMyEnum)))
{
   Console.WriteLine(val);
}

존 스키트 크레딧보다 수 있습니다. http://bytes.com/groups/net-c/266447-how-loop-each-items-enum

해설 (0)
foreach (Foos foo in Enum.GetValues(typeof(Foos)))
{
    ...
}
해설 (0)
  • 업데이트되도록 * 내가 보고 있는 저를 다시 나의 오랜 시간, 일부 셀명 누구이뇨 I& 다르게 # 39 라고 내가 생각하기에, d do it now. 요즘 I& # 39; d 쓰기:
private static IEnumerable GetEnumValues()
{
    // 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();
}
해설 (5)
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
}
해설 (0)
 Enum.GetValues(typeof(Foos))
해설 (0)