This translation from: How to Loop All through enum values in C # [Duplicate]?
This question already has an answer here: The problem here has the answer:
How do the I the enumerate AN enum in C #? How to enumerate enumeration in C #? Answers 26 26 answers
public enum Foos
{
A,
B,
C
}
A Way to Loop there IS through at The Possible values of Foos
? Is there a way cycle Foos
of possible values?
Basically? Basically?
foreach(Foo in Foos)
#1st Floor
Reference: https://stackoom.com/question/44wN/ how to traverse all enumeration values of C- - repeat
#2nd Floor
foreach(Foos foo in Enum.GetValues(typeof(Foos)))
#3rd floor
You CAN use at The yes GetValues
Method,: Yes, you can use the GetValues
method:
var values = Enum.GetValues(typeof(Foos));
Or the typed version: or 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: I long ago in such an occasion added a helper for my personal library:
public static class EnumUtil {
public static IEnumerable<T> GetValues<T>() {
return Enum.GetValues(typeof(T)).Cast<T>();
}
}
Usage: Usage:
var values = EnumUtil.GetValues<Foos>();
#4th floor
Yes. Yes. The Use GetValues()
Method in System.Enum
class. In System.Enum
using the class GetValues()
method.
#5th Floor
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
#6th floor
foreach (Foos foo in Enum.GetValues(typeof(Foos)))
{
...
}