Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Enum to List [duplicate]

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?

like image 969
Amit Avatar asked Jun 15 '13 12:06

Amit


People also ask

How to convert an enum to list?

The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array. To get a list, call ToList() after casting.

Can enum have duplicate values?

Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.

What does enum GetValues return?

The GetValues method returns an array that contains a value for each member of the enumType enumeration. If multiple members have the same value, the returned array includes duplicate values.

Can enum have duplicate values C++?

There is currently no way to detect or prevent multiple identical enum values in an enum.


1 Answers

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)); 
like image 121
It'sNotALie. Avatar answered Oct 08 '22 00:10

It'sNotALie.