Code Snippet: ListEnum

Written by Troy Howard

16 February 2007

Sometimes you want to list an Enum and see what it's actual numeric values are…

Well sometimes I do anyway, and when I do, I use:

private static void ListEnum(Type _enum) {

  Console.WriteLine("enum " \+ _enum.Name); 
  Console.WriteLine("{");

  string[] foo = Enum.GetNames(_enum); 
  Array bar = Enum.GetValues(_enum); 

  for(int i =0;i<foo.Length;i++) {
    Console.WriteLine( foo[i] + " = " \+ ((int)bar.GetValue(i)).ToString() + "," ); 
  }

  Console.WriteLine("}");
} 

Enjoy!