How to get all values of an enum?

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

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

(Family[])Enum.GetValues(typeof(Family))

Tags:

C#

Enums