Implement Sort and Custom Enumerator in Generic List
page 5 of 7
by Satheesh Babu
Feedback
Average Rating: 
Views (Total / Last 10 Days): 40802/ 70

Implementing Custom Enumerator

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.


View Entire Article

User Comments

Title: Comment box creation   
Name: Raghav
Date: 2011-11-01 1:43:04 PM
Comment:
I want to develop a comment box like face book in my website using ASP.net
Title: Mr   
Name: Banx
Date: 2010-10-06 6:59:59 PM
Comment:
With overload 2. Where does the comparecustomname method get the parameters from. its very confusing how it knows which parameters to use for the comparison.
Title: Implement Sort and Custom Enumerator in Generic List   
Name: Eric Ramírez
Date: 2010-04-09 11:58:27 PM
Comment:
The best article that i´ve found.

Thanks.
Title: Implement Sort and Custom Enumerator in Generic List   
Name: Saurabh
Date: 2008-12-25 12:16:29 PM
Comment:
Very nice explanation in a concise way...exactly to the point.
Title: Implement Sort and Custom Enumerator in Generic List   
Name: Michael
Date: 2008-04-12 10:30:13 PM
Comment:
Comprehensive and compact guide. And examples are actually working, in contrast to some MSDN articles
Title: Implement Sort and Custom Enumerator in Generic List   
Name: Wayde
Date: 2008-01-03 8:46:00 PM
Comment:
I have read a lot of stuff about custom enumeration and sorters and haven't understood any of it until I got to this article. Great Job! The article is concise, well organized, and the examples are fantastic.

Thank you.
Title: Implement Sort and Custom Enumerator in Generic List   
Name: Cheri
Date: 2008-01-03 3:02:25 AM
Comment:
cool article, learnt a lot of stuff from it






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-24 3:42:53 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search