【发布时间】:2021-07-01 22:32:17
【问题描述】:
我正在尝试了解如何实现泛型集合和 IEnumerator 接口;我正在使用Documentation provided to do so。
在给定的示例中,枚举器的方法 MoveNext() 实现如下:
public bool MoveNext()
{
//Avoids going beyond the end of the collection.
if (++curIndex >= _collection.Count)
{
return false;
}
else
{
// Set current box to next item in collection.
curBox = _collection[curIndex];
}
return true;
}
curIndex 用于作为BoxCollection 的索引,它实现了ICollection。如果我尝试做同样的事情,我会得到“无法使用 [] 将索引应用于 'System.Collections.Generic.ICollection... 类型的表达式”。
是文档有误,还是我做的不对?
【问题讨论】:
标签: c# generics ienumerator icollection