31

I want to create a method that takes in an Enum type, and returns all it's members in an array, How to create such a function?

Take for example, I have these two enums:

public enum Family
{ 
   Brother,
   Sister,
   Father
}

public enum CarType
{ 
   Volkswagen,
   Ferrari,
   BMW
}

How to create a function GetEnumList so that it returns

  1. {Family.Brother, Family.Sister, Family.Father} for the first case.
  2. {CarType.Volkswagen, CarType.Ferrari, CarType.BMW} for the second case.

I tried :

private static List<T> GetEnumList<T>()
{
    var enumList = Enum.GetValues(typeof(T))
        .Cast<T>().ToList();
    return enumList;
}

But I got an InvalidOperationException:

System.InvalidOperationException : Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true. at System.Reflection.RuntimeMethodInfo.ThrowNoInvokeException() at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)

Edit: The above code is working fine-- the reason I got an exception was because profiler caused me the bug. Thank you all for your solutions.

2
  • It worked fine for me - can you post the calling code? Commented Apr 29, 2009 at 5:58
  • 2
    Yes, it's working fine-- I found that my profiler actually cause me a bug and hence results in the exception. Commented Apr 29, 2009 at 6:08

5 Answers 5

30

Here is the full code:

    public enum Family
    {
        Brother,
        Sister,
        Father
    }

    public enum CarType
    {
        Volkswagen,
        Ferrari,
        BMW
    }


    static void Main(string[] args)
    {
        Console.WriteLine(GetEnumList<Family>());
        Console.WriteLine(GetEnumList<Family>().First());
        Console.ReadKey();
    }

    private static List<T> GetEnumList<T>()
    {
        T[] array = (T[])Enum.GetValues(typeof(T));
        List<T> list = new List<T>(array);
        return list;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

What is the difference between the following: (1)Enum.GetValues (typeof (theEnum)); // returns the pure enumeration defined in Enum (2)int i = (int) theEnum.item; // Indicator number from zero if not set and if they are set their value---- (3)Enum.GetNames (typeof (theEnum)); // Enumeration name returns as string
22

Same as the other answer but updated for modern C#:

public static List<TEnum> GetEnumList<TEnum>() where TEnum : Enum 
    => ((TEnum[])Enum.GetValues(typeof(TEnum))).ToList();

1 Comment

"Modern" is very short-lived. .Net core 5 has Enum.GetValues<TEnum>().
16
(Family[])Enum.GetValues(typeof(Family))

1 Comment

Thanks, but I want a general method that takes in enum type and return the respective list-- but a specific cast function.
8

Like this?

private static List<string> GetEnumList<T>()
{
    return Enum.GetNames( typeof( T ) )
           .Select(s => typeof(T).Name + "." + s).ToList();
}

Comments

3

Since .NET 5 you can do this:

public static T[] GetAll<T>() where T : struct, Enum
{
    return Enum.GetValues<T>();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.