Important Interface in .NET: Work With IEnumerator Interface

Welcome to the “Important Interface in C#” article series. In this series we will be talking about various useful interfaces of the .NET class library. In our previous article we talked about the IEnumerable, ICollection, IComparable, IList, ISerializable and many more interfaces, you can read them here.

In today’s article we will discuss the uses of the IEnumerator interface in the .NET class library. Though this interface performs well along with IEnumerator but we can understand the uses of the IEnumerator interface individually. Basically IEnumerator supports iteration over a non-generic collection.

Let’s see the location of the IEnumerator interface in the .NET class library.

Namespace: System.Collection
Assembly : mscorlib.dll

The prototype of the IEnumerator interface is like the following.

public interface IEnumerator

Properties

The interface contains only one property as in the following:

Property: Current Return current object from collection

Methods of IEnumerator interface

There are two methods in the IEnumerator interface. They are:

MoveNext: Advances the enumerator to the next element of the collection.

Reset: Sets the enumerator to its initial position, that is before the first element in the collection.

Now, let’s read data from the IList<T> collection and read using the MoveNext() method. Have a look at the following example.

using System;

using System.Collections;

using System.Collections.Generic;

using System.IO;

using System.Text;

namespace SelfHostingWebAPI

{

   class Program

    {

       public static void Print(IEnumerator<int> e)

       {

           while (e.MoveNext())

           {

               Console.WriteLine(e.Current);

           }

       }

 

        static void Main(string[] args)

        { 

            List<int> list = new List<int>();

            list.Add(10);

            list.Add(20);

            list.Add(30); 

            List<int>.Enumerator e = list.GetEnumerator();

            Program.Print(e);

            Console.ReadLine();

        }

    }

}

Here is the output of the example above. We are getting all integer numbers from the collection.

IEnumerator interface

Implement IEnumerator in user defined class

In this example we will implement IEnumerator in our user-defined function. As we have discussed earlier, if we implement IEnumerator in our class then we can use the functions and properties of the IEnumerator interface. In this class we have implemented the Reset() and MoveNext() methods. Try to understand the following implementation.
 

using System;

using System.Collections;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

 

namespace SelfHostingWebAPI

{

    public class Person :IEnumerator

    {

        public string name { get; set; }

        public string surname { get; set; }

 

        public Person[] per = null;

        public int Index = 0;

        public Person() { }

        public Person(Person [] p)

        {

            per = p;

        }

        public object Current

        {

            //Retunr current object

            get { return per[Index]; }

        }

        public bool MoveNext()

        {

            Index++;

            return (Index < per.Length);

        }

        public void Reset()

        {

            Index = 0;

        }

    }

   class Program

    {

        static void Main(string[] args)

        {

            Person p1 = new Person { name = "Sourav", surname = "Kayal" };

            Person p2 = new Person { name = "Ram", surname = "Kumar" };

            Person []p = new Person[2];

            p[0] = p1;

            p[1] =p2;

            Person p3 = new Person(p);

            Console.WriteLine(((Person)p3.Current).name + " " + ((Person)p3.Current).surname);

            p3.MoveNext();

            Console.WriteLine(((Person)p3.Current).name + " " + ((Person)p3.Current).surname);

            Console.ReadLine();

        }

    }

}

Here is the output of the example above. We are getting all the names that we have created in the person object collection.

Implement IEnumerator

Implement IEnumerator<T> in user defined class

Like many other interfaces, IEnumerator also has its parameterized version. In the following example we will implement IEnumerator<T> in our own class. Here is a sample implementation.
 

using System;

using System.Collections;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Runtime.Serialization;

using System.Text;

using System.Xml.Serialization;

using System.Globalization;

 

namespace SelfHostingWebAPI

{

    public class Person :IEnumerator<Person>

    {

        public string name { get; set; }

        public string surname { get; set; }

        public Person[] per = null;

        public int Index = 0;

        public Person() { }

        public Person(Person [] p)

        {

            per = p;

        }

        //public object Current

        //{

        //    //Retunr current object

        //    get { return per[Index]; }

        //}

        public bool MoveNext()

        {

            Index++;

            return (Index < per.Length);

        }

        public void Reset()

        {

            Index = 0;

        }

        Person IEnumerator<Person>.Current

        {

            get

            {

                return (Person) per[Index];

            }

        }

        public void Dispose()

        {

            per = null;

        }

        object IEnumerator.Current

        {

            get { throw new NotImplementedException(); }

        }

    }

   class Program

    {

        static void Main(string[] args)

        {

            Person p1 = new Person { name = "Sourav", surname = "Kayal" };

            Person p2 = new Person { name = "Manish", surname = "Khanra" };

            Person []p = new Person[2];

            p[0] = p1;

            p[1] =p2;

            IEnumerator<Person> pp = new Person(p);

            Console.WriteLine(((Person)pp.Current).name + " " + ((Person)pp.Current).surname);

            pp.MoveNext();

            Console.WriteLine(((Person)pp.Current).name + " " + ((Person)pp.Current).surname);

            Console.ReadLine();

        }

    }

}

The example is very similar to the previous example. The Current property has changed a little bit. And in the Main() function we are creating an object of IEnumerator<T> by the Person class. Here is sample output.

Implement IEnumerator in user defined class

Conclusion

In this article we have learned the concept of the IEnumerator interface in the .NET class library. I hope you have understood the basic uses of the IEnumerator interface. In the next article we will try to discuss one more important interface of the .NET class library. Happy coding.


Similar Articles