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.