Converting Array to IEnumerable

后端 未结 2 1379
伪装坚强ぢ
伪装坚强ぢ 2021-02-13 00:36

To my surprise, I get the following statement:

public static IEnumerable AllEnums 
  => Enum.GetValues(typeof(SomeType));

to

2条回答
  •  星月不相逢
    2021-02-13 01:07

    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 AllEnums
        = (SomeType[])Enum.GetValues(typeof(SomeType));
    

    or perhaps simpler:

    public static SomeType[] AllEnums 
        = (SomeType[])Enum.GetValues(typeof(SomeType));
    

提交回复
热议问题