将数组转换为IEnumerable< T> [英] Converting Array to IEnumerable<T>
问题描述
令我惊讶的是,我得到以下语句:
To my surprise, I get the following statement:
public static IEnumerable<SomeType> AllEnums
=> Enum.GetValues(typeof(SomeType));
来抱怨无法将 System.Array em> System.Collection.Generic.IEnumerable 。我认为后者是从前者继承的。显然我错了。
to complain about not being able to convert from System.Array to System.Collection.Generic.IEnumerable. I thought that the latter was inheriting from the former. Apparently I was mistaken.
因为我不能LINQ它或 .ToList 它,我不知道如何正确处理。我宁愿避免显式转换,并且因为它是一个枚举的值,我不认为像SomeType - 它将是很有用的,既不是。
Since I can't LINQ it or .ToList it, I'm not sure how to deal with it properly. I'd prefer avoiding explicit casting and, since it's a bunch of values for an enum, I don't think as SomeType-ing it will be of much use, neither.
推荐答案
一般的 Array
基类不是类型,所以不实现任何类型特定的接口;然而,可以直接投射向量 - GetValues
实际返回一个向量;所以:
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));
或更简单:
public static SomeType[] AllEnums
= (SomeType[])Enum.GetValues(typeof(SomeType));
这篇关于将数组转换为IEnumerable< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!