- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.2k
Description
I suggest to implement Generic version of GetValues method in the Enum class.
In present, if we going to get an typed array of some enum values we have to write following code:
SomeEnum[] values = (SomeEnum[])Enum.GetValues(typeof(SomeEnum));
It seems as unconvenient way.
In my opinion, the following Generics-based syntax should seems shorter, more convenient and safely (by reason of type casting necessity):
SomeEnum[] values = Enum.GetValues<SomeEnum>();
The possible way to implement this syntax is:
public static TEnum[] GetValues<TEnum>() where TEnum : struct
{
    Type enumType = typeof(TEnum);
    return (TEnum[])enumType.GetEnumValues();
}
This proposal is inspired by the great Generic-based version of Enum.TryParse method laying besides with legacy non-Generic version:
public static bool TryParse(Type enumType, String value, out Object result)
public static bool TryParse<TEnum>(String value, out TEnum result) where TEnum : struct
If proposal to implement Generic version of GetValues will be accepted I guess following Generics-based implementations of GetName GetNames should be implemented for symmetry reason:
public static String GetName<TEnum>(Object value) where TEnum : struct
{
    Type enumType = typeof(TEnum);
    return enumType.GetEnumName(value);
}
public static String[] GetNames<TEnum>() where TEnum : struct
{
    Type enumType = typeof(TEnum);
    return enumType.GetEnumNames();
}
I introduce possible implementation of this proposal in dotnet/coreclr/pull#16557
This is a small point improvement suggestion.
It can be implemented separately of as a part of total Enum improvements discussing in dotnet/corefx/#15453