Ienumerable and Ienumerator interface implementation c#



IEnumerable and IEnumerator are two interfaces that are used to implement iteration in. NET. In C#, all collections such as lists

using System;
using System.Collections;

namespace CSharpCollection
{
    public class Watch
    {
        internal byte hour, minute;
		internal string name;
    }

	class WatchArray : IEnumerable
	{
		Watch[] RoyalWatches = new Watch[4];
		
		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"};
		}
		
		public IEnumerator GetEnumerator()
		{
		// Return the array object's IEnumerator.
			return RoyalWatches.GetEnumerator();
		}
	}
	
    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);
			}
        }
    }
}

0 Comment's

Comment Form

Submit Comment