How to loop through all enum values in C#? [duplicate]
问题 This question already has answers here : Closed 7 years ago . This question already has an answer here: How do I enumerate an enum in C#? 26 answers public enum Foos { A, B, C } Is there a way to loop through the possible values of Foos ? Basically? foreach(Foo in Foos) 回答1: Yes you can use the GetValues method: 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