If you need speed and type checking at build and run time, this helper method is better than using LinqLINQ to cast each element:
public static T[] GetEnumValues<T>() where T : struct, IComparable, IFormattable, IConvertible
{
if (typeof(T).BaseType != typeof(Enum))
{
throw new ArgumentException(string.Format("{0} is not of type System.Enum", typeof(T)));
}
return Enum.GetValues(typeof(T)) as T[];
}
andAnd you can use it like below:
static readonly YourEnum[] _values = GetEnumValues<YourEnum>();
ofOf course you can return IEnumerable<T>, but that buys you nothing here.