42

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?

1
  • 3
    Take a look at Enum.GetValues. Commented Jun 15, 2013 at 12:20

4 Answers 4

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));
Sign up to request clarification or add additional context in comments.

Comments

14

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

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

2 Comments

Why do OfType<Language>().ToList() when you can just cast it straight to a Languages[]?
@newStackExchangeInstance Yep you are correct
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();

Comments

-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());
    }
  }

1 Comment

That's way too complicated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.