0

I have an enum which has a few aliases:

enum {
   A = 0,
   B = 1,
   OtherB = 1,
   ...
}

I'm trying to iterate over all values of the enumeration using Enum.GetValues. However, I need the string representation of the enum values, not the integer values. Just iterating over Enum.GetValues, I get a sequence A, B, B, ... (or for some enums A, OtherB, OtherB, ..., but regardless I only get one of each value).

Is it possible to get all the "String-values" of an enum, or are they removed during compilation?

carlpett
  • 10,619
  • 4
  • 42
  • 80

2 Answers2

4
Enum.GetNames(typeof(WhatEverEnum))

http://msdn.microsoft.com/en-us/library/system.enum.getnames(v=vs.110).aspx

Stilgar
  • 20,008
  • 6
  • 58
  • 93
1
            foreach(var str in Enum.GetNames(typeof(myEnum)))
            {
                // use str
                Console.WriteLine(str);
            }
Ashok Damani
  • 3,774
  • 4
  • 26
  • 42