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?
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?
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));
If I understand your requirement correctly , you are looking for something like this
var enumList = Enum.GetValues(typeof(Language)).OfType<Language>().ToList();
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();
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());
}
}