如果您的基础属性是Array
,则可以使用Array.GetLength(int dimension)
来获取指定维度中数组的长度,但在这种情况下,您可以简单地使用其内置的枚举器。
这工作,例如:
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
}
查找yield语句。这与嵌套for循环相结合应该得到你想要的。 – 2011-05-05 07:32:31