To my surprise, I get the following statement:
public static IEnumerable AllEnums
=> Enum.GetValues(typeof(SomeType));
to
I thought that the latter was inheriting from the former.
Enum.GetValues
returns Array
, which implements the non-generic IEnumerable
, so you need to add a cast:
public static IEnumerable<SomeType> AllEnums = Enum.GetValues(typeof(SomeType))
.Cast<SomeType>()
.ToList();
This works with LINQ because Cast<T>
extension method is defined for the non-generic IEnumerable
interface, not only on IEnumerable<U>
.
Edit: A call of ToList()
avoid inefficiency associated with walking multiple times an IEnumerable<T>
produced by LINQ methods with deferred execution. Thanks, Marc, for a great comment!
The general Array
base class is not typed, so it does not implement any type-specific interfaces; however, a vector can be cast directly - and GetValues
actually returns a vector; so:
public static IEnumerable<SomeType> AllEnums
= (SomeType[])Enum.GetValues(typeof(SomeType));
or perhaps simpler:
public static SomeType[] AllEnums
= (SomeType[])Enum.GetValues(typeof(SomeType));