We will create two classes, namely SeniorCustomerEnumerator
and JuniorCustomerEnumerator that implements IEnumerable<Customer>
interface like below for iterating the customer list and fulfilling our
requirement.
Listing 17 - Enumerator or Iterator implementation
public class SeniorCustomerEnumerator: IEnumerable < Customer >
{
List < Customer > list = new List < Customer > ();
public SeniorCustomerEnumerator(List < Customer > emplist)
{
list = emplist;
}
public IEnumerator < Customer > GetEnumerator()
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].Age > 50)
{
yield return list[i];
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (GetEnumerator());
}
}
public class JuniorCustomerEnumerator: IEnumerable < Customer >
{
List < Customer > list = new List < Customer > ();
public JuniorCustomerEnumerator(List < Customer > emplist)
{
list = emplist;
}
public IEnumerator < Customer > GetEnumerator()
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].Age < 50)
{
yield return list[i];
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (GetEnumerator());
}
}
We have created Custom enumerator class with the use of
yield keyword and now it is time to update the CustomerCollection class that
gives the enumerator object for iterating.
Listing 18 - Custom Enumerator or Iterator
implementation
public class CustomerCollection
{
List < Customer > list = new List < Customer > ();
public List < Customer > Customers
{
set
{
list = value;
}
}
public CustomerCollection(List < Customer > cus)
{
this.Customers = cus;
}
public SeniorCustomerEnumerator GetSeniorCustomerEnumerator()
{
SeniorCustomerEnumerator enume = new SeniorCustomerEnumerator(list);
return enume;
}
public JuniorCustomerEnumerator GetJuniorCustomerEnumerator()
{
JuniorCustomerEnumerator enume = new JuniorCustomerEnumerator(list);
return enume;
}
}
The GetSeniorCustomerEnumerator() and GetJuniorCustomerEnumerator()
can be used to iterate through the generic list and give the customers with age
> 50 and age < 50 respectively. The following code shows how to use "eth"
above custom iterator.
Listing 19 - Using the Custom Enumerator
CustomerCollection cuscoll = new CustomerCollection(cuslist);
Console.WriteLine("Senior Customers ");
SeniorCustomerEnumerator sen = new SeniorCustomerEnumerator(cuslist);
foreach (Customer cus in cuscoll.GetSeniorCustomerEnumerator())
{
Console.WriteLine(cus.Name + " " + cus.Age);
}
Console.WriteLine("Junior Customers ");
foreach (Customer cus in cuscoll.GetJuniorCustomerEnumerator())
{
Console.WriteLine(cus.Name + " " + cus.Age);
}
The output of the above code will be:
Senior Customers
Fatima 57
Evangeline 52
Cameroon 55
Junior Customers
Damien 49
Babu 24
We can also use the default GetEnumerator() method and
filter the employees with age> 50 and age < 50.
Listing 20 - Other Way of Enumerating
Console.WriteLine("Senior Customer");
foreach (Customer cus in cuslist)
{
if (cus.Age > 50)
{
Console.WriteLine(cus.Name + " " + cus.Age);
}
}
Console.WriteLine("Junior Customer");
foreach (Customer cus in cuslist)
{
if (cus.Age < 50)
{
Console.WriteLine(cus.Name + " " + cus.Age);
}
}
We can create custom enumerator using the yield keyword when
we implement our own custom collection and if it needs an iterator for
filtering members specific to a business need like above.