Hello kenneth,
You should study the structure of the iteration interfaces - IEnumerable
is an interface that contains a method GetEnumerator, which in turn
returns an IEnumerator.
You'd normally have a structure like this:
class MyCollection : IEnumerable {
public IEnumerator GetEnumerator() {
// This method is implemented to satisfy the requirements
// of the IEnumerable interface
}
}
The reason you do this is because you would like to use an instance of
your class for iteration, like this:
MyCollection coll = ...;
foreach (Item item in coll) ...
As you can see, the iteration works directly with your instance, which is
because the IEnumerable interface is implemented. Obviously, if you want
to do other iterations, you'll have to have an IEnumerable interface for
these as well, so you do:
class MyCollection : IEnumerable {
...
public IEnumerable AnotherSortOrder() { ... }
}
MyCollection coll = ... ;
foreach (Item item in coll.AnotherSortOrder()) ...
In this case you're calling a method to get the iteration interface - of
course it still has to be the right type.
So the short summary is: the foreach wants to have an IEnumerable to
iterate over. Your class can implement that interface itself and/or it can
contain methods returning that type. It depends on your purpose and on the
syntax you want to use for the iteration.
Oliver Sturm
--
http://www.sturmnet.org/blog