How do I convert the following Enum to a List of strings?
[Flags]
public enum DataSourceTypes
{
None = 0,
Grid = 1,
ExcelFile = 2,
ODBC = 4
};
I couldn't find this exact question, this Enum to List is the closest but I specifically want List<string>
Use
Enum
's static method,GetNames
. It returns astring[]
, like so:If you want to create a method that does only this for only one type of
enum
, and also converts that array to aList
, you can write something like this:I want to add another solution: In my case, I need to use a Enum group in a drop down button list items. So they might have space, i.e. more user friendly descriptions needed:
In a helper class (HelperMethods) I created the following method:
When you call this helper you will get the list of item descriptions.
ADDITION: In any case, if you want to implement this method you need :GetDescription extension for enum. This is what I use.