Monday, June 22, 2009

IEnumerable and IEnumerator

In the .NET Framework, these two interfaces are designed to allow you to iterate easily over collections of objects as you would typically do in a for loop. Many classes in the .NET Framework have implemented these interfaces and do their work behind the scenes so we don’t have to worry about how it is done.

Any classes implementing these two interfaces are classified as an enumerable collection. This means that User-defined class behaves like any other collection object written by Microsoft, such as an ArrayList.

There are many features that define an enumerable collection, and by implementing these two interfaces, the framework guarantees that this object has these features. For example, an enumerable collection must have a method called MoveNext, which moves the cursor within the collection one step forward. By implementing the IEnumerator interface, the class is promising (and is required) to implement this method.

IEnumerator and IEnumerable interfaces are defined in System.Collections namespace.

IEnumerator

MSDN describes the IEnumerator class as "Supports a simple iteration over a collection.", as well as:

"IEnumerator is the base interface for all enumerators. Enumerators only allow reading the data in the collection. Enumerators cannot be used to modify the underlying collection.

Initially, the enumerator is positioned before the first element in the collection. Reset also brings the enumerator back to this position. At this position, calling Current throws an exception. Therefore, you must call MoveNext to advance the enumerator to the first element of the collection before reading the value of Current."

IEnumerator is an interface that has following members:

-> Property: Current
-> Method: MoveNext
-> Method: Reset

Presumably, any element in a collection can be retrieved through its index property. But instead of element index, the IEnumerator provides two abstract methods and a property to pull a particular element in a collection. And they are Reset(), MoveNext() and Current.

public interface IEnumerator
{
bool MoveNext(); //An enumerator is always positioned before the
//first element of the collection, first call to MoveNext
//moves the enumerator over the first element of the
//collection
object Current { get ; } //Returns current object from the collection
void Reset(); //Resets enumerator to just above the first element of the collection.
}

IEnumerable

IEnumerable is an interface that has 1 method called GetEnumerator. The MSDN documentation describes the GetEnumerator method as "Exposes the enumerator, which supports a simple iteration over a collection."... It then goes on to say "IEnumerable must be implemented to support the ForEach semantics of Microsoft Visual Basic. COM classes that allow enumerators also implement this interface".

IEnumerable is an interface that has following member:

-> Method: GetEnumerator

public interface IEnumerable
{
//The signature of GetEnumerator() is as follows:
public IEnumerator GetEnumerator()
{
//return IEnumerator of our Custom Type
return(IEnumerator)this;
}
}

Note: IEnumerator interface is meant to be used as accessors and is not helpful to make any changes in the collection or elements of the collection.

0 comments: