C# 如何实现IEnumerable<;T>;在具有多维数组内部列表的集合上?
我有一个收藏:C# 如何实现IEnumerable<;T>;在具有多维数组内部列表的集合上?,c#,.net,collections,multidimensional-array,ienumerable,C#,.net,Collections,Multidimensional Array,Ienumerable,我有一个收藏: interface IPinCollection : IEnumerable<IPin> { IPin this[int [,] indices] { get; set; } } 接口IPinCollection:IEnumerable { IPin这个[int[,]索引]{get;set;} } 基本上,它有一个作为矩阵的内部列表,它的每个[rowindex,columnIndex]中都有IPin实例 我想要实现的是能够使用for..遍历此矩阵的所有IPin实
interface IPinCollection : IEnumerable<IPin>
{
IPin this[int [,] indices] { get; set; }
}
接口IPinCollection:IEnumerable
{
IPin这个[int[,]索引]{get;set;}
}
基本上,它有一个作为矩阵的内部列表,它的每个[rowindex,columnIndex]中都有IPin实例
我想要实现的是能够使用for..遍历此矩阵的所有IPin实例
您能为我推荐一种线程安全、简单且快速的方法来实现IEnumerable吗?如果您的基础属性是
数组,您可以使用它来获取指定维度中数组的长度,尽管在这种情况下,您可以简单地使用它的内置枚举器
这是有效的,例如:
int[,] arr = new int[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
{ 10, 11, 12 }
};
foreach (int i in arr)
Console.WriteLine(i);
这意味着您只需按照数组枚举器返回值的顺序从数组中返回值:
class PinCollection : IPinCollection
{
private IPin[,] _array;
#region IEnumerable<int> Members
public IEnumerator<int> GetEnumerator()
{
foreach (IPin i in _array)
yield return i;
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
foreach (IPin i in _array)
yield return i;
}
#endregion
}
class-PinCollection:IPinCollection
{
专用IPin[,]_数组;
#区域可数成员
公共IEnumerator GetEnumerator()
{
foreach(IPin i在_数组中)
收益率i;
}
#端区
#区域可数成员
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
foreach(IPin i在_数组中)
收益率i;
}
#端区
}
如果基础属性是一个数组,则可以使用获取指定维度中数组的长度,不过在这种情况下,只需使用其内置枚举器即可
这是有效的,例如:
int[,] arr = new int[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
{ 10, 11, 12 }
};
foreach (int i in arr)
Console.WriteLine(i);
这意味着您只需按照数组枚举器返回值的顺序从数组中返回值:
class PinCollection : IPinCollection
{
private IPin[,] _array;
#region IEnumerable<int> Members
public IEnumerator<int> GetEnumerator()
{
foreach (IPin i in _array)
yield return i;
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
foreach (IPin i in _array)
yield return i;
}
#endregion
}
class-PinCollection:IPinCollection
{
专用IPin[,]_数组;
#区域可数成员
公共IEnumerator GetEnumerator()
{
foreach(IPin i在_数组中)
收益率i;
}
#端区
#区域可数成员
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
foreach(IPin i在_数组中)
收益率i;
}
#端区
}
如果数组的维度已知,例如私有只读IPin[,]数据
,那么实际上非常简单(因为您已经可以在多维数组上使用foreach
;但是请注意,T[*,*]
本身并不实现IEnumerable
,因为这在技术上不是foreach
的要求):
public IEnumerator GetEnumerator()
{
foreach(IPin引脚中的引脚)产生返回引脚;
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
如果数组的维度已知,例如私有只读IPin[,]数据
,那么实际上非常简单(因为您已经可以在多维数组上使用foreach
;但是请注意,T[*,*]
本身并不实现IEnumerable
,因为这在技术上不是foreach
的要求):
public IEnumerator GetEnumerator()
{
foreach(IPin引脚中的引脚)产生返回引脚;
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
查找收益率报表。这与嵌套for循环相结合,应该可以满足您的需要。与嵌套for循环相结合应该可以得到您想要的结果。+1用于记录foreach循环不需要IEnumerable。运行时实际上使用duck类型:)@MattDavey-no,编译器使用duck类型;p但实际上,在这种情况下,它使用IEnumerable
(非泛型)。+1来记录foreach循环不需要IEnumerable。运行时实际上使用duck类型:)@MattDavey-no,编译器使用duck类型;p但实际上,在本例中,它使用的是IEnumerable
(非泛型)。