What is the need for Iterator Pattern in C#?

Iterator pattern is used to iterate over a set of elements irrespective of their internal representation. The elements will be iterated in sequential order.



Here is the sample interface of an iterator:

public interface sampleIterator {
object firstElement();
object nextElement();
bool isIterationDone();
object currentElement();
}
Purpose of each of the methods in the above interface is shown below:

• firstElement() – Returns the first element of the iterator
• nextElement() – Returns the next element in the iterator
• currentElement() – Returns the current element from the iterator
• isIterationDone() – Returns true if the iteration is done, returns false otherwise

Usage of iterator pattern has few consequences. They are listed below:

• The methods firstElement, nextElement and currentElement will be returning Object rather than more specific type. You have to explicitly cast the resultant Object to the corresponding type.
• Sometimes you might be iterating through a set of elements that are getting changed. This might give incorrect data result.
• Ensure that iterator has privileged access to iterate over the collection of elements.


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| What is the need for Bridge Pattern in C#? | What is the need for Builder Pattern in C#? | What is the need for Chain of Responsibility Pattern in C#? | What is the need for Command Pattern in C#? | What is the need for Composite Pattern in C#? | What is the need for Decorator Pattern in C#? | What is the need for Flyweight Pattern in C#? | What is the need for Interpreter Pattern in C#? | What is the need for Iterator Pattern in C#? | What is the need for Mediator Pattern in C#? | What is the need for Memento Pattern in C#? | What is the need for Prototype Pattern in C#? | What is the need for State Pattern in C#? | What is the need for Strategy Pattern in C#? | What is the need for Template Method Pattern in C#? | What is the need for unsafe code in C#? | What is the purpose of assert() in C#? | What is the purpose of AutoResetEvent in .NET? | What is the purpose of Console.ReadLine() in C#? | What is the purpose of machine.config file in .NET? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2023 - All Rights Reserved.