Iterator

What Does Iterator Mean?

An iterator, in the context of C#, is a block of code that returns an ordered sequence of values of a collection or array. It is a member function implemented using the iterator block, which contains one or more statements containing the "yield" keyword.

Advertisements

An iterator is used to enable consumers of a container class, containing a collection or array, to traverse the collection using the "foreach" statement in a simpler manner. It is used with strongly typed collection classes to iterate complex data structures like binary trees, which require recursive traversal and maintain the iteration state through the recursion. The concept of iterator is also used in implementing deferred execution in LINQ queries.

Techopedia Explains Iterator

The iterator is based on a design pattern that provides a method to traverse the elements of a collection of items sequentially without exposing the underlying structure of the collection. It eliminates the tedious task of implementing the "IEnumerator" interface manually when creating collection classes that support the "foreach" statement. The Intermediate Language code necessary for implementing the methods and properties of the IEnumerator interface is generated by the compiler, which results in simpler syntax, reduced code size, and improvement in developer productivity.

In general, an iterator is similar to a database cursor in that it provides access to data elements in a collection, but does not perform iteration. An iterator can be implemented in C# as a method, operator, or get accessor. For example, an iterator can be used to traverse a collection of strings to display the content of each string in the collection.

An iterator is represented by the IEnumerator interface and is implemented by the compiler with the following methods:

  • MoveNext: A method that advances to the next element of the collection and indicates the end of that collection
  • Current: A property that fetches the value of the element currently being pointed to
  • Dispose: Cleans up the iteration

GetEnumerator() is the default iterator method of the IEnumerable interface. This method can be implemented in the collection that contains the container class. The GetEnumerator() method is invoked on execution of the "foreach" statement, which uses the returned enumerator to iterate through values. The "yield return" statement is used within the iterator block to fetch an element of the collection. It also helps to position the current location such that execution starts from this location the next time it occurs. The "yield break" statement ends the iteration.

Advertisements

Related Terms

Margaret Rouse

Margaret Rouse is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical, business audience. Over the past twenty years her explanations have appeared on TechTarget websites and she's been cited as an authority in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine and Discovery Magazine.Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages. If you have a suggestion for a new definition or how to improve a technical explanation, please email Margaret or contact her…