How to create a list of strings based on enum type items in c#?

Advertisements

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.

>Solution :

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

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

Leave a Reply Cancel reply