Home > Back-end >  How to create a list of strings based on enum type items in c#?
How to create a list of strings based on enum type items in c#?

Time:01-19

I have an enum type like this:

public enum Colors
    {
        Black,
        Brown,
        Red,
        Orange,
        Yellow,
        Green,
        Blue,
        Violet,
        Grey,
        White
    }

And I want to create an array of strings based on items in this enum type. Like this:

strint[] mylist = {"Black", "Brown", "Red", ... }

Could anyone help me? Thanks.

CodePudding user response:

It's pretty easy, since the Enum class provides a method for that:

string[] mylist = Enum.GetNames(typeof(Colors));
  • Related