Home » c# » Convert an enum to List<string>

Convert an enum to List<string>

Posted by: admin November 30, 2017 Leave a comment


How do I convert the following Enum to a List of strings?

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 a string[], 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 a List, you can write something like this:

public List<string> GetDataSourceTypes()
    return Enum.GetNames(typeof(DataSourceTypes)).ToList();


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:

  public enum CancelReasonsEnum
    [Description("In rush")]
    [Description("Need more coffee")]
    [Description("Call me back in 5 minutes!")]

In a helper class (HelperMethods) I created the following method:

 public static List<string> GetListOfDescription<T>() where T : struct
        Type t = typeof(T);
        return !t.IsEnum ? null : Enum.GetValues(t).Cast<Enum>().Select(x => x.GetDescription()).ToList();

When you call this helper you will get the list of item descriptions.

 List<string> items = HelperMethods.GetListOfDescription<CancelReasonEnum>();

In any case, if you want to implement this method you need :GetDescription extension for enum. This is what I use.

 public static string GetDescription(this Enum value)
        Type type = value.GetType();
        string name = Enum.GetName(type, value);
        if (name != null)
            FieldInfo field = type.GetField(name);
            if (field != null)
                DescriptionAttribute attr =Attribute.GetCustomAttribute(field,typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attr != null)
                    return attr.Description;
        return null;
        /* how to use
            MyEnum x = MyEnum.NeedMoreCoffee;
            string description = x.GetDescription();