我正在尝试了解如何实现泛型集合和 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...’类型的表达式。
文档有误,还是我做错了什么?