116

I have an enum that I'd like to display all possible values of. Is there a way to get an array or list of all the possible values of the enum instead of manually creating such a list? e.g. If I have an enum:

public enum Enumnum { TypeA, TypeB, TypeC, TypeD }

how would I be able to get a List<Enumnum> that contains { TypeA, TypeB, TypeC, TypeD }?

Termininja
  • 6,620
  • 12
  • 48
  • 49
Mark LeMoine
  • 4,478
  • 3
  • 31
  • 52
  • 4
    possible duplicate of [convert enum to list in c#](http://stackoverflow.com/questions/1167361/convert-enum-to-list-in-c) – Mark Byers Sep 28 '10 at 20:30

11 Answers11

201

This gets you a plain array of the enum values using Enum.GetValues:

var valuesAsArray = Enum.GetValues(typeof(Enumnum));

And this gets you a generic list:

var valuesAsList = Enum.GetValues(typeof(Enumnum)).Cast<Enumnum>().ToList();
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • 6
    This makes me always wonder why `Enumnum.GetValues()` and `Enumnum.GetNames()` doesn't exist. – dalle Sep 28 '10 at 20:38
  • 11
    For an array variable where the element type is known at compile-time, use `var valuesAsArray = (Enumnum[])Enum.GetValues(typeof(Enumnum));`. – Jeppe Stig Nielsen Sep 22 '13 at 19:51
  • Or `Enum.GetValues(typeof(Enumnum)).Cast().ToList();` – mr5 May 19 '17 at 10:46
  • @mr5: In your example, what should `ActualType` mean? For an enum, it would be `Enumnum` in the OP's example, or it could be the underlying type (usually `int`). Did you have anything else in mind? – Dirk Vollmar May 19 '17 at 12:50
  • If you want an ordinary array, use Enum.GetValues(typeof(TSomeEnum)).Cast().ToArray(); Enum.GetValues returns System.Array Or use direct cast: (TSomeEnumType[])Enum.GetValues(typeof(TSomeEnumType)) This is the shorted syntax. If you want an array the answer above shows this. – Tore Aurstad Jan 17 '20 at 14:55
  • As written in another answer this now has a generic overload. `Enum.GetValues();` – Kim Jun 27 '23 at 11:51
20

Try this code:

Enum.GetNames(typeof(Enumnum));

This return a string[] with all the enum names of the chosen enum.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
11

This is way easier now with the generic method in .NET 5.0.

ColorEnum[] colors = Enum.GetValues<ColorEnum>();

MS Doc: Enum.GetValues

Jordan Ryder
  • 2,336
  • 1
  • 24
  • 29
8
Enum.GetValues(typeof(Enumnum));

returns an array of the values in the Enum.

duraz0rz
  • 387
  • 1
  • 2
  • 10
7

You may want to do like this:

public enum Enumnum { 
            TypeA = 11,
            TypeB = 22,
            TypeC = 33,
            TypeD = 44
        }

All int values of this enum is 11,22,33,44.

You can get these values by this:

var enumsValues = Enum.GetValues(typeof(Enumnum)).Cast<Enumnum>().ToList().Select(e => (int)e);

string.Join(",", enumsValues) is 11,22,33,44.

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55
5

You can use

Enum.GetValues(typeof(SomeEnum)).Cast<SomeEnum>().ToArray();

This returns an array!

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
4

Something little different:

typeof(SomeEnum).GetEnumValues();
Termininja
  • 6,620
  • 12
  • 48
  • 49
2

with this:

string[] myArray = Enum.GetNames(typeof(Enumnum));

and you can access values array like so:

Array myArray = Enum.GetValues(typeof(Enumnum));
Dr TJ
  • 3,241
  • 2
  • 35
  • 51
2

If you prefer a more generic way, here it is. You can add up more converters as per your need.

    public static class EnumConverter
    {

        public static string[] ToNameArray<T>()
        {
            return Enum.GetNames(typeof(T)).ToArray();
        }

        public static Array ToValueArray<T>()
        {
            return Enum.GetValues(typeof(T));
        }

        public static List<T> ToListOfValues<T>()
        {
            return Enum.GetValues(typeof(T)).Cast<T>().ToList();
        }


        public static IEnumerable<T> ToEnumerable<T>()
        {
            return (T[])Enum.GetValues(typeof(T));
        }

    }

Sample Implementations :

   string[] roles = EnumConverter.ToStringArray<ePermittedRoles>();
   List<ePermittedRoles> roles2 = EnumConverter.ToListOfValues<ePermittedRoles>();
   Array data = EnumConverter.ToValueArray<ePermittedRoles>();
Ozesh
  • 6,536
  • 1
  • 25
  • 23
1

The OP asked for How to get an array of all enum values in C# ?

What if you want to get an array of selected enum values in C# ?

Your Enum

    enum WeekDays 
    {
        Sunday, 
        Monday,
        Tuesday
    }

If you want to just select Sunday from your Enum.

  WeekDays[] weekDaysArray1 = new WeekDays[] { WeekDays.Sunday };

  WeekDays[] weekDaysArray2 = Enum.GetValues(typeof(WeekDays)).Cast<WeekDays>().Where
  (x => x == WeekDays.Sunday).ToArray();

Credits goes to knowledgeable tl.

References:

1.

2.

Hope helps someone.

Shaiju T
  • 6,201
  • 20
  • 104
  • 196
0

also you can use

var enumAsJson=typeof(SomeEnum).Name + ":[" + string.Join(",", Enum.GetValues(typeof(SomeEnum)).Cast<SomeEnum>().Select(e => e.ToString())) + "]";

for get all elements in enum as json format.

Meysam
  • 21
  • 2