>. 이 질문에 답을 이미 here: >. 26 대답 https://stackoverflow.com/questions/105372/how-to-enumerate-an-enum
텍스트 - 끝 - < 자동으로 삽입됨 >;!
public enum Foos
{
A,
B,
C
}
반복할 수 있는 방법이 푸 값을 통해 '?'
기본적으로?
foreach(Foo in Foos)
예 's' 제바루이 방법을 사용할 수 있습니다.
var values = Enum.GetValues(typeof(Foos));
또는 입력되었는지 버전:
var values = Enum.GetValues(typeof(Foos)).Cast<Foos>();
오래 전에 내가 그냥 내 전용 library) 을 계기로 같은 보조 기능을 추가했다.
public static class EnumUtil {
public static IEnumerable<T> GetValues<T>() {
return Enum.GetValues(typeof(T)).Cast<T>();
}
}
사용법:
var values = EnumUtil.GetValues<Foos>();
foreach (EMyEnum val in Enum.GetValues(typeof(EMyEnum)))
{
Console.WriteLine(val);
}
존 스키트 크레딧보다 수 있습니다. http://bytes.com/groups/net-c/266447-how-loop-each-items-enum
foreach (Foos foo in Enum.GetValues(typeof(Foos)))
{
...
}
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>();
}
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
}