As you can see, here we define Employee class. This class has three properties – FirstName, LastName and Age –and
override method ToString which
returns string representation of class.
Now,
we’ll define Enumerable class for Employee called EmployeeCollection which implements System.Collection.IEnumerable interface. This class contains Array of Employee class as its data member. This member initializes in
constructor as show below
public EmployeeEnumerable(Employee[] listEmployee)
{
employess = new Employee[listEmployee.Length];
Array.Copy(listEmployee, employess, listEmployee.Length);
}
In this example we are implementing IEnumerable interface explicitly, reason
of doing this is while using GetEnumerator
method, I want user only get my version of GetEnumerator
method. Let see how to do this:
// This method implemented explicitly, that's why it's
// not visible to user. So user will always use second
// version of this method.
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator() as IEnumerator;
}
// User can use this version only
public EmployeeEnumerator GetEnumerator()
{
return new EmployeeEnumerator(employess);
}
So, this way we can hide IEnumerable’s method implementation
with our own version.
Till
now we had developed Enumerable class for our own Class. Now we are going to
define Enumerator class for our entity. Below we’ve code for Enumerator class.
class EmployeeEnumerator : IEnumerator
{
Employee[] employess;
int position = -1;
public EmployeeEnumerator(Employee[] lstEmployee)
{
employess = lstEmployee;
}
public Employee Current
{
get
{
try
{
return employess[position];
}
catch (IndexOutOfRangeException) { throw new InvalidCastException(); }
}
}
object IEnumerator.Current
{
get { return Current; }
}
public bool MoveNext()
{
position++;
return position < employess.Length;
}
public void Reset()
{
position = -1;
}
}
You can see here also we are implementing methods of IEnumerator interface explicitly. Hope this
class is self describable hence need not to write much specification. So new we
are moving how to use this concept. Here is code that uses this classes.
static class Enumerator
{
[STAThread]
static void Main(string[] args)
{
Employee[] list = new Employee[]{
new Employee() { FirstName="Himanshu", LastName="Manjarawala", Age=30},
new Employee(){ FirstName="Hetal", LastName="Sangani", Age=26},
new Employee(){ FirstName="Viral", LastName="Sangani", Age=32},
new Employee(){ FirstName="Rajesh", LastName="Patel", Age=29},
new Employee(){ FirstName="Nehal", LastName="Thakkar", Age=30}
};
var enumerable = new EmployeeCollection(list);
var enumerator = enumerable.GetEnumerator();
while (enumerator.MoveNext())
{
Employee e = enumerator.Current;
Console.WriteLine(e.ToString());
}
}
}
Here, we’ve define Array
of our own Employee class, you can define List<>
as well. As we know these both are of System.Collection
type of object, so they internally support Enumerable methods. We instantiate
our own EmployeeCollection class and
then from there we’ll get our Enumerator
object for Employee class using GetEnumerator() method and from that
object we can iterate over our array members.
Hope you liked this type of concept of iterator pattern.