Iterator: Implementation and Example
Learn the Iterator design pattern by implementing an example program.
Creating a console application
We’ll create a console application project. The first thing that we’ll add to this project is the following interface:
Press + to interact
namespace Iterator_Demo;internal interface IIterator{bool MoveNext();int GetCurrent();}
This is our Iterator interface. It has two methods: MoveNext()
and GetCurrent()
. The MoveNext()
method will iterate through each collection item and will update the pointer to the current item. It will return true
if it’s possible to move to the next item, and false
if there are no more items left.
GetCurrent()
will retrieve the current value from the collection. For simplicity, ...