首页 > 解决方案 > 什么 IEnumerable> 是什么意思?

问题描述

我见过的 c# 中的一些方法需要IEnumerable<IEnumerable<"some class">>. 当方法需要 IEnumerable<"some class">作为参数时,我可以传递一维数组或列表。我认为通过相同的逻辑我可以将二维数组传递为IEnumerable<IEnumerable<"some class">>,但我的编译器说它不一样。例如,我可以将哪些数据类型传递给此类方法?

标签: c#

解决方案


例如IEnumerable<IEnumerable<int>>是一个整数枚举的枚举。

https://www.codingame.com/playgrounds/213/using-c-linq---a-practical-overview/ienumerablet

https://docs.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1

这意味着根列表的每个元素都是一个 int 列表。

如果我们写:

IEnumerable<IEnumerable<int>> listOfLists = GetItems();

我们可以像这样解析项目:

foreach ( var list in listOfLists )
{
  Console.WriteLine("Values");
  foreach ( var value in list )
    Console.WriteLine("    " + value);
}

事实上,如果我们声明:

var items = new List<List<int>>();

这是一个IEnumerable<IEnumerable<int>>这里。

它就像一个 int 数组的数组:

var items = int[][];

这里它不是一个多维数组,而是一个锯齿状数组:

https://docs.microsoft.com/dotnet/csharp/programming-guide/arrays/jagged-arrays

https://www.tutorialsteacher.com/csharp/csharp-jagged-array

https://www.c-sharpcorner.com/UploadFile/puranindia/jagged-arrays-in-C-Sharp-net


推荐阅读