HosseinSedghian
HosseinSedghian

Reputation: 385

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

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.

Upvotes: 3

Views: 75

Answers (1)

Rufus L
Rufus L

Reputation: 37020

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

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

Upvotes: 3

Related Questions