数组作为 IEnumerable 实例

所有阵列都实现非通用的 IList 接口(因此非通用的 ICollectionIEnumerable 基接口)。

更重要的是,一维数组实现了 IList<>IReadOnlyList<> 通用接口(及其基接口),用于它们包含的数据类型。这意味着它们可以被视为通用的可枚举类型,并传递给各种方法,而无需先将它们转换为非数组形式。

int[] arr1 = { 3, 5, 7 };
IEnumerable<int> enumerableIntegers = arr1; //Allowed because arrays implement IEnumerable<T>
List<int> listOfIntegers = new List<int>();
listOfIntegers.AddRange(arr1); //You can pass in a reference to an array to populate a List.

运行此代码后,列表 listOfIntegers 将包含一个包含值 3,5 和 7 的 List<int>

IEnumerable<> 支持意味着可以使用 LINQ 查询数组,例如 arr1.Select(i => 10 * i)