How to get an array of all enum values in C#?

C#Enums

C# Problem Overview


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 }?

C# Solutions


Solution 1 - C#

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();

Solution 2 - C#

Try this code:

Enum.GetNames(typeof(Enumnum));

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

Solution 3 - C#

Enum.GetValues(typeof(Enumnum));

returns an array of the values in the Enum.

Solution 4 - C#

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.

Solution 5 - C#

Something little different:

typeof(SomeEnum).GetEnumValues();

Solution 6 - C#

You can use

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

This returns an array!

Solution 7 - C#

with this:

string[] myArray = Enum.GetNames(typeof(Enumnum));
and you can access values array like so:
Array myArray = Enum.GetValues(typeof(Enumnum));

Solution 8 - C#

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>();

Solution 9 - C#

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.

Solution 10 - C#

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

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

MS Doc: Enum.GetValues

Solution 11 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark LeMoineView Question on Stackoverflow
Solution 1 - C#Dirk VollmarView Answer on Stackoverflow
Solution 2 - C#Øyvind BråthenView Answer on Stackoverflow
Solution 3 - C#duraz0rzView Answer on Stackoverflow
Solution 4 - C#Ali SoltaniView Answer on Stackoverflow
Solution 5 - C#TermininjaView Answer on Stackoverflow
Solution 6 - C#Mitchel SellersView Answer on Stackoverflow
Solution 7 - C#Dr TJView Answer on Stackoverflow
Solution 8 - C#OzeshView Answer on Stackoverflow
Solution 9 - C#Shaiju TView Answer on Stackoverflow
Solution 10 - C#Jordan RyderView Answer on Stackoverflow
Solution 11 - C#ModernNetView Answer on Stackoverflow