如何实现 IEnumerable在具有多维数组内部列表的集合上?
我有一个集合:
interface IPinCollection : IEnumerable<IPin>
{
IPin this[int [,] indices] { get; set; }
}
基本上它有一个内部列表作为矩阵,每个 [rowindex,columnIndex] 中都有 IPin 实例。
我想要实现的是能够使用 for..each 遍历该矩阵的所有 IPin 实例。
您能否建议我一种线程安全、简单且快速的方法来实现 IEnumerable 来实现此目的?
I have a collection:
interface IPinCollection : IEnumerable<IPin>
{
IPin this[int [,] indices] { get; set; }
}
Basically it has an innerlist as matrix which has IPin instance in its each [rowindex,columnIndex].
What I want to achieve is to be able to walkthrough the all IPin instances of this matrix with for..each.
Can you suggest me a thread-safe,simple and quick way to implement IEnumerable to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的基础属性是
Array
,则可以使用Array.GetLength(intDimension)
获取指定维度中数组的长度,尽管在这种情况下您可以简单地使用其内置枚举器。这是有效的,例如:
这意味着您可以简单地从数组中返回值,按照其枚举器返回它们的顺序:
If your underlying property is an
Array
, you can useArray.GetLength(int dimension)
to get the length of the array in the specified dimension, although in that case you can simply use its built-in enumerator.This works, for example:
It means you can simply return values from the array, in the order its enumerator returns them:
如果数组的维度已知,例如
private readonly IPin[,] data;
,那么实际上非常简单(因为您已经可以在多维数组上foreach
;但请注意,T[*,*]
本身并不实现IEnumerable
- 因为这在技术上不是< /em>foreach
的要求):If the array is of known dimension, for example
private readonly IPin[,] data;
, then actually pretty simply (since you can alreadyforeach
over a multi-dimensional array; but note thatT[*,*]
doesn't itself implementIEnumerable<T>
- since that isn't technically a requirement forforeach
):