IEnumerator happens to be an interface which will let you iterate through a list once it is implemented in a program. For this, IEnumerator can avail of two methods – Reset and MoveNext. IEnumerator also has the Current property.

As should be perceptible, Reset takes the control to the beginning of the list while MoveNext will increment by one through the list. This also somewhat specifies a basic quality of IEnumerator, that it can only be used to read the data in a collection, but it cannot be used to update the data in the collection.

Difference Between IEnumerator And IEnumerable Explained

IEnumerable too is an interface. It is after IEnumerable has been implemented that you can use the ‘For each’ loop. The difference between IEnumerable and IEnumerate can be summed up like this: IEnumerable is something that can be enumerated. For that, it has the GetEnumerator method which returns an IEnumerator.

The one reason you’d like to implement IEnumerator is when you have something where you have to enumerate, and you have to explicitly define how is that to be done. To do that, you have to create a new class to implement IEnumerator though you will have to return the IEnumerator in an IEnumerable class.

The following code snippet is an example of how IEnumerable and IEnumerator are used in a program.

Create a simple class, call it book

public class Book
{
    public Book(string title, string author)
    {
        this.title =  title;
        this.author = author;
    }

    public string title;
    public string author;
}

Now create another Books class ( with s). This class implements IEnumerable. IEnumerable allows the class to use the ForEeach when implemented and used.

public class Books : IEnumerable
{
    private Book[] _book;
    public Books(Book[] bArray)
    {
        _book = new Book[bArray.Length];

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

    IEnumerator IEnumerable.GetEnumerator()
    {
        return (IEnumerator)GetEnumerator();
    }

    public BooksEnum GetEnumerator()
    {
        return new BooksEnum(_book);
    }
}

Now create a class BooksEnum, which implements IEnumerator

public class BooksEnum : IEnumerator
{
    public Book[] _book;
 
    int position = -1;

    public BooksEnum(Book[] list)
    {
        _book = list;
    }

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

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

    object IEnumerator.Current
    {
        get
        {
            return Current;
        }
    }

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

Now implement the class in the main class

    static void Main()
    {
        Book[] booksArray = new Book[2]
        {
            new Book("Clean Code", "Robert Cecil Martin"),
            new Book("Programming Pearls", "Jon Bentley"),
        };

        Books booksList = new Books(booksArray);
        foreach (Book b in booksList)
            Console.WriteLine("Book Title:" + b.title + ". Book Author:" + b.author);

    }

Output:

Book Title: Clean Code. Book Author: Robert Cecil Martin
Book Title: Programming Pearls. Book Author: Jon Bentley

Related Articles

Last modified: November 11, 2019

Comments

Write a Reply or Comment

Your email address will not be published.