Search code examples
c#ienumerablemenuilist

Converting an Enum type to IList


Hi
How can I convert an enum to IList?
Suppose you want to read all elements in FormWindowState enum, and return an IList<FormWindowState> containing Normal, Minimized and Maximized


Solution

  • Use Enum.GetValues(). For example:

    var list = (IList<FormWindowState>) Enum.GetValues(typeof(FormWindowState));
    

    It actually returns a FormWindowState[] but that implements IList<T> appropriately anyway.

    If you're doing a lot of work with enums and you want a more type-safe approach, you may want to look at my Unconstrained Melody project too.