Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

enums

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.

like image 865
HosseinSedghian Avatar asked Dec 04 '25 01:12

HosseinSedghian


1 Answers

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

string[] mylist = Enum.GetNames(typeof(Colors));
like image 90
Rufus L Avatar answered Dec 06 '25 13:12

Rufus L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!