How to iterate over and Enum?

  • Thread starter Thread starter garth
  • Start date Start date
G

garth

Hi,
Is it possible to iterate over and enumerated type in C#?
If so please can you paste a simple exampel up that covers the whole
process.

Thanks
 
Do you mean as in:
// enumerate values, finding names
foreach(MyEnum value in Enum.GetValues(typeof(MyEnum))) {
// or ToString()?
string name = Enum.GetName(typeof(MyEnum), value);
// do something
}
// enumerate names, finding values
foreach (string name in Enum.GetNames(typeof(MyEnum))) {
MyEnum value = (MyEnum) Enum.Parse(typeof(MyEnum), name);
// do something
}
Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top