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

Using Anonymous Methods

C# 2.0 introduced a concept called Anonymous methods which we can use in the place of a delegate. An anonymous method is a method without any name. To understand better, a normal method is one which will have a name, return type optionally arguments and an access modifier. So, an anonymous method in C# 2.0 is a feature to have methods without name and it can be used in the place where there is a use of a delegate. I will implement anonymous method to sort the generic list in this section.

List.Sort (Generic Comparison) using Anonymous method:

I am creating List<Customner> with 5 customers and using Anonymous method to sort the list.

The above implementation of Generic Comparison delegate in Listing 8 - List creation and Sorting can be rewritten using Anonymous delegate.

Listing 15 - Anonymous method implementation         

Console.WriteLine("\nSort Based on Name");
 
Cuslist2.Sort(delegate(Customer c1, Customer c2)
{
  return c1.Name.CompareTo(c2.Name);
}
 
);
foreach (Customer cus in cuslist2)
{
  Console.WriteLine(cus.Name + " " + cus.Age);
}
 
Console.WriteLine("\nSort Based on Age");
 
Cuslist2.Sort(delegate(Customer c1, Customer c2)
{
  return c1.Age.CompareTo(c2.Age);
}
 
);
foreach (Customer cus in cuslist2)
{
  Console.WriteLine(cus.Name + " " + cus.Age);
}

The output will be:

Sort Based on Name
Mathew 27
Nguyen 21
Peter 20
Sanjay 24
Tom 23
Sort Based on Age
Peter 20
Nguyen 21
Tom 23
Sanjay 24
Mathew 27

We have implemented sorting for the generic list using all the 4 overloads.

In the coming sections we will implement custom enumeration feature for our generic list. However, the Generic list have its own enumerating feature that is exposed via GetEnumerator() method. This section will also use the same Customer object that is used in the above discussions. We will create a class called CustomerCollection.

Listing 16 - CustomerCollection Object

public class CustomerCollection
{
  List < Customer > list = new List < Customer > ();
  public List < Customer > Customers
  {
    set
    {
      list = value;
    }
  }
  public CustomerCollection(List < Customer > cus)
  {
    this.Customers = cus;
  }
}

To make the understanding simple we will create a CustomerCollection class that takes List<Customer> list and we will create a custom enumerator implementation in C# for iterating the generic list using one of the new keyword of C#2.0 called yield. For example, what if the user needs to get all the customers with age > 50 and customers with age < 50 for some business reason? In this case we need to iterate the list, check the age and can output the customer details. Like the general GetEnumerator() method in List<T>, we will implement 2 enumerators called GetSeniorCustomerEnumerator() and GetJuniorCustomerEnumerator() that fetches employees with age >50 and age < 50 respectively. To implement the enumerator we need to implement IEnumerable<T> interface.

What does the yield keyword do?

Yield keyword is used in GetEnumerator() method of IEnumerable interface and we do not need to explicitly implement the MoveNext(), Reset() and Current of IEnumerator interface like we do in 1.x days. Thus, C# is doing some the work on behalf of us when we use yield keyword. With this information we will jump into the implementation of custom enumerator.


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-25 4:27:02 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search