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> GetEnumerator() {...