ALERT

[FREE DEMO] Deploy Your Enterprise Cloud in Minutes

Iterator

Definition - 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.

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.
This definition was written in the context of C#

Techopedia Deals

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
Tweat cdn.techopedia.com
"Techopedia" on Twitter


'@Techopedia'
Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join thousands of others with our weekly newsletter

Resources
The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.