This article, explains how to use the Iterator pattern to manipulate any collection of objects. To explain this I am using two interfaces IEnumerator and IEnumerables.
Iterator pattern:
In object-oriented programming, the Iterator pattern is a design pattern in which iterators are used to aggregate object sequentially without exposing its underlying representation. An Iterator object encapsulates the internal structure of how the iteration occurs.
I have a class called Employee, which stores his ID and name.
public class Employee
{
private int m_nID;
private string m_strName;
public Employee(int nID,string strName)
m_nID = nID;
m_strName = strName;
}
#region Property
/// <summary>
///
/// </summary>
public int EmployeeID
get
return this.m_nID;
public string EmployeeName
return this.m_strName;
# endregion
I have two more classes
public class EnumerateEmployee: IEnumerator
private int m_nPosition;
private ArrayList m_ArrEmployee = new ArrayList();
/// </summary>d
public EnumerateEmployee()
m_nPosition = -1;
m_ArrEmployee.Add(new Employee(1,"Kamsa"));
m_ArrEmployee.Add(new Employee(2,"Rama"));
m_ArrEmployee.Add(new Employee(3,"Sita"));
m_ArrEmployee.Add(new Employee(4,"Gopala"));
#region EnumMembers
/// <returns></returns>
public bool MoveNext()
bool b_return = false;
++m_nPosition;
if(m_nPosition < m_ArrEmployee.Count)
b_return = true;
return b_return;
public object Current
return m_ArrEmployee[m_nPosition];
public void Reset()
In order to execute this class, create a window application and put the following code in the button click event.
string strName;int nID;EmployeeList objEmpList = new EmployeeList();IEnumerator objEnumEmp = objEmpList.GetEnumerator();
while(objEnumEmp.MoveNext())
Employee objEmployee = (Employee)objEnumEmp.Current;
nID = objEmployee.EmployeeID; strName = objEmployee.EmployeeName;
Exploring Design Pattern for Dummies