advertisement
Iterators in C# Programming
Iterators
Consider a scenario where a person is trying to memorize a book of 100 pages. To finish the task, the person must iterate through each 100 pages.
Similar to this person who iterates through the pages, an iterator in C# is used to traverse through a list a collection. It is a block of code that uses the for each loop to refer to a collection of values sequentially. For example, consider a collection of values that must be sorted. To implement the logic manually, a programmer can iterate through each value sequentially using iterators to compare the values.
An iterator is not a data member but is a way of accessing the member. It can be a method, a get accessor, or an operator that allows you to navigate through the values in a collection. Iterators specify how values are generated when the foreach statement accesses the elements within a collection. They keep track of the elements in the collection, so you can retrieve these values if required.
For example, consider an array variable consisting of six elements, where the iterator can return all the elements within an array one by one.
1. Benefits of iterator
For a class that behaves like a collection, it is preferable to use iterators to iterate through the values of the collection the for each statement. By doing this, one can get the following benefits:
- Iterators provide a simplified and faster way of iterating through the values of a collection.
- Iterators reduce the complexity of providing an enumerator for a collection.
- Iterators can return a large number of values.
- Iterators can be used to evaluate and return only those values that are required.
- Iterators can return values without consuming memory by referring to each value in the list.
2. Implementation of iterator
Iterators Can be created by implementing the Get Enumerator() method that returns a reference of the IEnumerator interface.
The iterator block uses the yield keyword to provide values to the instance of the enumerator or to terminate the iteration. The yield return statement returns the values, while the yield break statement ends the iteration process. When the program control reaches the yield return statement, the current location is stored, and the next time the iterator is called, the execution is started from the stored location.
3. Generic Iterators
C# allows programmers to create generic iterators. Generic iterators are created by returning an object of the generic IEnumerator<T> or IEnumerable<T> interface. They are used to iterate through values of any value type.
4. Implementing Named Iterators
Another way of creating iterators is by creating a method, whose return type is the IEnumerable interface. This is called a named iterator. Named iterators can accept parameters that can be used to manage the starting and end points of a each loop. This flexible technique allows you to fetch the required values from the Collection.
advertisement
Conversation
Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!
Join the discussion and share your insights now!
Comments 0