c# - 什么 IEnumerable> 是什么意思?
问题描述
我见过的 c# 中的一些方法需要IEnumerable<IEnumerable<"some class">>
. 当方法需要 IEnumerable<"some class">
作为参数时,我可以传递一维数组或列表。我认为通过相同的逻辑我可以将二维数组传递为IEnumerable<IEnumerable<"some class">>
,但我的编译器说它不一样。例如,我可以将哪些数据类型传递给此类方法?
解决方案
例如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
推荐阅读
- azure - 向 videoindexer 提供一个 videoUrl,其中包含查询参数
- solr - Apache solr 使用分片分发搜索不起作用 (shardsWhitelist)
- java - 实现一个 3X3 矩阵的队列
- python - Python - 减少重复列表,但如果它们是交替的,则保留列表中的重复
- ruby - 包含 Vagrantfile 的配置文件目录
- sql - 如何获取一个ID的最新2条记录
- python - 如何根据另一个数据框的值在现有数据框上创建新列,这两个数据框的长度都不同
- excel - 如何检查超链接是否打开 PDF 或在 Excel 中返回“未找到”错误
- c# - .net 5.0 库可用于 VBA
- python - CondaError:尝试使用 conda 下载 cudnn 时,下载的字节与 Content-Length 不匹配