Output name/values of an Enum

Wednesday, March 28, 2007 Labels: , ,

I took the time a while ago to figure out the best way to output each name and value in an Enum. I just keep using the code and have been looking in recent projects looking for the code to re-use in a new project. So, I thought I’d share the love..

This code will output the name and value of each element in the MyEnum object.

string[] names = Enum.GetNames(typeof(MyEnum));
Array values = Enum.GetValues(typeof(MyEnum));
for (int i = 0; i < names.Length; i++)
Console.WriteLine(string.Format("{0} = {1}", names[i], values.GetValue(i)));

I think Reflection is probably the most overlooked and under-appreciated assembly in the whole .Net framework. Don’t you just love it? 8^)
Older Post Home Newer Post