41

Say i have the following Enum Values

enum Language
    {
       CSharp= 0,
        Java = 1,
        VB = 2

    }

I would like to convert them to list of values (i.e) { CSharp,Java,VB}.

How to convert them to a list of values?

Pramod More
  • 1,220
  • 2
  • 22
  • 51
Amit
  • 447
  • 1
  • 6
  • 8
  • 3
    Take a look at [Enum.GetValues](http://msdn.microsoft.com/library/system.enum.getvalues.aspx). – Corak Jun 15 '13 at 12:20

4 Answers4

53
Language[] result = (Language[])Enum.GetValues(typeof(Language))

will get you your values, if you want a list of the enums.

If you want a list of the names, use this:

string[] names = Enum.GetNames(typeof(Languages));
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
14

If I understand your requirement correctly , you are looking for something like this

var enumList = Enum.GetValues(typeof(Language)).OfType<Language>().ToList();
9

If you want to store your enum elements in the list as Language type:

Enum.GetValues(typeof(Language)).Cast<Language>().ToList();

In case you want to store them as string:

Enum.GetValues(typeof(Language)).Cast<Language>().Select(x => x.ToString()).ToList();
monrow
  • 169
  • 1
-1

You can use this code

  static void Main(string[] args)
  {
   enum Days { Sat, Sun, Mon, Tue, Wed, Thu, Fri };

    Array arr = Enum.GetValues(typeof(Days));
    List<string> lstDays = new List<string>(arr.Length);
    for (int i = 0; i < arr.Length; i++)
    {
        lstDays.Add(arr.GetValue(i).ToString());
    }
  }
Shyam sundar shah
  • 2,473
  • 1
  • 25
  • 40