Iterator
A way provided by this pattern lets the client access the aggregate object in turns(one by one) but without exposing the details of the aggregate object.
An iterator are designed in this pattern to enumerate the object.
Auctually. ASP.NET has implemented this pattern with providing two interfaces:
- IEnumerator: does not support the generics
- IEnumerable: support the generics
Most of time for most of situations they are enough for our program. there is no need for us to do it ourself. But it is ok for us to dig, which can give us more.
The iterator is responsible for enumerating the aggregate object and accesing the inner types of the object. The client just uses the Iterator to MoveNext. to access the First.etc.
If you want a object to be accessed by the index, implement the indexer provied by the C#, and also implement the Iterator(customed or inherent ones), the interator will hold the object and make the enumeration if needed.
Investigating the history is for further understanding of the future.
I made a demo implementing the Iterator pattern, and also supporting the generics, see below:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
One more thing, I strongly recommend every one should use the generics supported from the C#2 for security issues for performance issue.