Iterator pattern in the .NET BCL

The iterator pattern is so common that most platforms and frameworks provide a mechanism to support it. The .NET BCL has got IEnumerable and its generic variant , that is, IEnumerable<T> to implement custom iterators. To iterate, we have got the foreach loop construct in C#. Similar constructs are available in Java as well. The following program creates a custom list by leveraging the .NET fixed length array facility:

 public class CustomList<T> : IEnumerable<T> { //------ A Fixed Length array to //------ Example very simple T[] _Items = new T[100]; int next_Index = 0; public CustomList(){} public void Add(T val) { // We are adding value without robust // error checking _Items[next_Index++] = val; } public IEnumerator<T> ...

Get .NET Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.