Basic things first about interface.
Think of interface to mean 'feature list' or 'list of capabilities'.
When a class implements an interface, it actually means that class
will have those 'features' or 'capabilities' listed in that interface.
But this time, those features are not just listed but instead are
actually coded ( implemented in code).
If you get to understand this. You will see that
IEnumerator is an inteface that gives the ability to work with a
collection of objects. It has the 'list of features' so that you can
go through each object in the collection. The IEnumerator simply
specifies what are the things you need to do this. If you look at
the documentation is has these members
1. Current - pick current object
2. MoveNext - go to the next object
3. Reset - go back start all over again
Hey, think of it. Those simple three are just ENOUGH to get things
done. There is nothing more you need!!!!! That's serves exactly the
purpose of the interface.
Ok so much for the blah blah. Actual code now!
This article discusses and compares both versions in NET 1.X and .NET
2.0
http://www.codeguru.com/csharp/.net/...le.php/c11193/
---- VERSION 1.X
class IteratorTest : IEnumerable
{
private ArrayList items = new ArrayList();
public int Count
{
get { return items.Count; }
}
public object this[int index]
{
get { return items[index]; }
set { items.Insert(index, value); }
}
public IEnumerator GetEnumerator()
{
return new IteratorSampleEnumerator(this);
}
private class IteratorSampleEnumerator : IEnumerator
{
IteratorTest aggregate = null;
int index = -1;
public IteratorSampleEnumerator(IteratorTest aggregate)
{
this.aggregate = aggregate;
}
public virtual object Current
{
get { return this.aggregate[index]; }
}
public virtual bool MoveNext()
{
index++;
if (index >= this.aggregate.Count)
{
return false;
}
else
{
return true;
}
}
public virtual void Reset()
{
index = -1;
}
}
--------- VERSION 2.0
class IteratorTest2<T> : IEnumerable
{
private ArrayList items = new ArrayList();
public int Count
{
get { return items.Count; }
}
public T this[int index]
{
get { return (T)items[index]; }
set { items.Insert(index, value); }
}
public IEnumerator GetEnumerator()
{
foreach (T item in this.items)
{
yield return item;
}
}
}