A look at IEnumerator and IEnumerable interfaces in C#

There are two interfaces which are at the base of many classes and the logic we use daily, to implement .Net based applications. These two interfaces are closely related to each other and provide a fuctionality that makes working with a Collection, fun. These are interfaces are IEnumerator and IEnumerable

Both of these interfaces work together. We need a class to implement IEnumerator interface, which provides implementations for Current property , MoveNext() and Reset() methods like below.

First, consider a class Job, around which all the logic will surround

    public class Job
    {
        public Job(string _jobName, int _duration)
        {
            this.jobName = _jobName;
            this.duration = _duration;
        }

        public string jobName;
        public int duration;
    }

Now, we have class that implements IEnumerator with an underlying array of Job objects:

    class JobsList : IEnumerator
    {
        Job[] jobs;
        int position = -1;

        public JobsList(Job[] _jobs)
        {
            jobs = _jobs;
        }

        #region IEnumerator Members

        public object Current
        {
            get
            {
                try
                {
                    return jobs[position];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }

        public bool MoveNext()
        {
            position++;
            return (position < jobs.Length);
        }

        public void Reset()
        {
            position = -1;
        }

        #endregion
    }

Now consider another class Jobs. This class implements IEnumerable interface method GetEnumerator() returns IEnummerator object which holds indexed access to underlying array of Job objects as follows:

    class Jobs : IEnumerable
    {
        #region IEnumerable Members

        Job[] jobs;

        public Jobs(Job[] _jobsArray)
        {
            jobs = new Job[_jobsArray.Length];

            for (int i = 0; i < _jobsArray.Length; i++)
            {
                jobs[i] = _jobsArray[i];
            }
        }

        public IEnumerator GetEnumerator()
        {
            return new JobsList(jobs);
        }

        #endregion
    }

This is how now can use and manipulate our newly created IEnumerator object

        static void Main(string[] args)
        {
            // create new Job Array
            Job[] jobsArray = 
                 new Job[] { new Job("JobA", 1), new Job("JobB", 2), new Job("JobC", 3) };

            // // create new IEnumerable 
            Jobs jobs = new Jobs(jobsArray);

            // // create new IEnumerator
            IEnumerator jobsEnummerator = jobs.GetEnumerator();

            // following are some you can work over 
            // ways we can works over IEnumerator object
            while (jobsEnummerator.MoveNext())
            { }

            //  or using foreach
            foreach (Job job in jobs)
            { }
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s