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?
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.
Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.
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.
There is currently no way to detect or prevent multiple identical enum values in an enum.
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With