IEnumerator allows read-only access to a collection. IEnumerator has MoveNext and Reset methods and the current to get the current record.
using System; using System.Collections; namespace CSharpCollection { public class Watch { internal byte hour, minute; internal string name; } class WatchArray : IEnumerator, IEnumerable { int index = -1; Watch[] RoyalWatches = new Watch[4]; //Collection Items public WatchArray() { RoyalWatches[0] = new Watch(){hour=12, minute=0, name="HMT"}; RoyalWatches[1] = new Watch(){hour=10, minute=10, name="SONATA"}; RoyalWatches[2] = new Watch(){hour=5, minute=40, name="RADO"}; RoyalWatches[3] = new Watch(){hour=3, minute=0, name="TITAN"}; } //Implementation of IEnumerable Interface's GetEnumerator() Method public IEnumerator GetEnumerator() { return this; } //Implementation of IEnumerator Interface's GetEnumerator() Method // Return the current object. public object Current { get { return RoyalWatches[index]; } } // Advance to the next object. public bool MoveNext() { if(index == RoyalWatches.Length-1) { Reset(); // reset enumerator at the end return false; } index++; return true; } // Reset the enumerator to the start. public void Reset() { index = -1; } } class UsingWatchArray { public static void Main(string[] args) { WatchArray Watches = new WatchArray(); foreach(Watch x in Watches) { Console.WriteLine("Time of {0} - {1}:{2}", x.name, x.hour, x.minute); } } } }