I'm trying to understand how to implement generic collections and the IEnumerator interface; I'm using the Documentation provided to do so.
In the given example the enumerator's method MoveNext() is implemented as follows:
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
is used to as index for BoxCollection
, which implements ICollection
. If I try to do the same I get "Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.ICollection...".
Is the documentation wrong, or is it me not doing something correctly?
BoxCollection
itself implements the indexer:
public Box this[int index]
{
get { return (Box)innerCol[index]; }
set { innerCol[index] = value; }
}
(line 129-133 of the sample you linked to)
You're right that you can't use the indexer on a class that implements ICollection<T>
- unless that class also implements an indexer.