Enumerators in C#


Description 

This article explains how to implement enumerators in C#.

We know how to use foreach for built-in types of C#.But how do we Use for objects of our class. For

class
Employee
{
}

how do we use foreach for the objects of Employee. 

Step#1:

To foreach construct for user-defined classes the first thing we have to do is Imlplement the interface IEnumerable. Then Ienumerable interface has a method called GetEnumerator() which returns an object of IEnumerator. 

Step#2:

Next we have to implement IEnumerator interface.The IEnumerator has 3 methods namely Reset(),Current(),MoveNext().After we code for the 3 methods we are able to use foreach() for our UDC(User Defined Classes).

Step#3:

How does foreach works when we use foreach() construct it first Calls MoveNext() if it returns true then the loop is continued to the value from the Current() method call.Reset() is used to set the Enumerator to begining position. 

Let's run through a coding sample which will make things clear:

The source code has 2 classes namely Employee,Employees. Employee class hold information about each Employee. The Employees class hold array of Employee objects.

Source Code Explanation:

  1. Create Employees class object

    Employees EmpList=
    new Employees();

  2. Create Employee class objects

    Employee e1=
    new Employee(1,"Employee#1",1250.75);
    Employee e2=
    new Employee(2,"Employee#2",1275.85);

  3. Add Employee object to Employees class object

    EmpList.AddEmployee(e1);
    EmpList.AddEmployee(e2);

  4. foreach(Employee emp in EmpList)
    {
    Console.WriteLine(emp);
    }

    When this statement is executed it will call the GetEnumerator() method of the Employees class to get the IEnumerator object.Then it calls the MoveNext() method since it returns true it calls the Current() method and return the Employee object.which is printed.this same sequence is repeated for 2nd Employee.During the third time call MoveNext() returns false and it exits the foreach construct. 

  5. To clearly get the underhood of Enumerator() see the code below which does the above sequence:

    IEnumerator EmpEnumerator=EmpList.GetEnumerator(); //Getting the Enumerator
    EmpEnumerator.Reset(); //Position at the Beginning
    While(EmpEnumerator.MoveNext()) //Till not finished do print
    {
    Console.WriteLine((Employee)EmpEnumerator.Current);
    }
    }

Complete Source Code:

// Source Code starts here
using System;
using System.Collections;
class Employee
{
private int Id;
private string Name;
private double Salary;
public Employee(int id,string name,double salary)
{
this.Id=id;
this.Name=name;
this.Salary=salary;
}
public int ID
{
get
{
return this.Id;
}
}
public override string ToString()
{
return "Id:" + this.Id.ToString() + "\nName:" + Name + "\nSalary:" + Salary.ToString();
}
}
class Employees:IEnumerable,IEnumerator
{
ArrayList EmpList=
new ArrayList();
private int Position=-1;
public void AddEmployee(Employee oEmp)
{
EmpList.Add(oEmp);
}
/* Needed since Implementing IEnumerable*/
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
/* Needed since Implementing IEnumerator*/
public bool MoveNext()
{
if(Position<EmpList.Count-1)
{
++Position;
return true;
}
return false;
}
public void Reset()
{
Position=-1;
}
public object Current
{
get
{
return EmpList[Position];
}
}
public static void Main()
{
Employees EmpList=
new Employees();
Employee e1=
new Employee(1,"Employee#1",1250.75);
Employee e2=
new Employee(2,"Employee#2",1275.85);
EmpList.AddEmployee(e1);
EmpList.AddEmployee(e2);
Console.WriteLine("Enumerating Employees using foreach");
foreach(Employee emp in EmpList)
{
Console.WriteLine(emp);
}
Console.WriteLine("\nEnumerating Employee Creating object of IEnumerator");
IEnumerator EmpEnumerator=EmpList.GetEnumerator();
EmpEnumerator.Reset();
while(EmpEnumerator.MoveNext())
{
Console.WriteLine((Employee)EmpEnumerator.Current);
}
}
}
// Source Code Ends here

Up Next
    Ebook Download
    View all
    Learn
    View all