Wednesday, May 19, 2010

Using Extension methods on Lists to make it fluent

Since C# 3.0 you can write extension methods on any class that you want, even if you don’t have the source code of the class. In other words you can extend a class with your own methods.

Extension methods can only be declared in static classes as static methods.

Extensions on List<T> classes can be very handy and make your code more readable and fluent.
Suppose you have this mixed Animal list with Dogs and Cats from this blogpost.

List<Animal> animalList = new List<Animal>(); 
animalList.Add(new Dog("Dog1", ConsoleColor.Red));
animalList.Add(new Dog("Dog2", ConsoleColor.Green));
animalList.Add(new Dog("Dog3", ConsoleColor.Red));
animalList.Add(new Cat("Cat1", ConsoleColor.Black));
animalList.Add(new Cat("Cat2", ConsoleColor.Black));
animalList.Add(new Cat("Cat3", ConsoleColor.Black));

(Never mind the ConsoleColor for the animal ;-) )

Suppose we want all the red dogs from the animalList, we could write our code like:


//Get the Red docs

List<Dog> redDogList = animalList.OfType<Dog>().Where(n => n.Color == ConsoleColor.Red).ToList();

This works, however everytime we want to get some colored animal we would get the same type of code. We would probably write some static methods to avoid this :


 List<Dog> redDogList = ListUtils.GetTheDogs(animalList);


Even better is to make this static extension methods so that the functionality looks to be encapsulated by the list its self. We can then also make the method names more readable, for instant not “GetTheDog”, but “WhichAreDog”.


For example:
public static List<Animal> WhichAreDog(this List<Animal> animalList)
{
  return animalList.OfType<Dog>().Cast<Animal>().ToList();
}



public static
List<Animal> HavingColor(this List<Animal> animalList, ConsoleColor color)
{
  return animalList.Where(n => n.Color == color).ToList();
}

The “this” before the first parameter of the method indicates that this is an extension method on this parameter, in this case the list with animals.


Now getting the red dogs looks like this:


List<Animal> allRedDogs = 
animalList.WhichAreDog().HavingColor(ConsoleColor.Red);

Much more readable and fluent!

5 comments:

Kyle A. Miller said...

Delphi Prism does this better.

Roland Beenhakker said...

@Kyle A. Miller In what way do you find it better? Syntax?

Anonymous said...

I cannot thank Mr Benjamin service enough and letting people know how grateful I am for all the assistance that you and your team staff have provided and I look forward to recommending friends and family should they need financial advice or assistance @ 1,9% Rate for Business Loan .Via Contact : .  lfdsloans@outlook.com. WhatsApp...+ 19893943740. Keep up the great work.
Thanks, Busarakham.

360DigiTMG Artificial intelligence Malaysia said...


You really make it look so natural with your exhibition however I see this issue as really something which I figure I could never understand. It appears to be excessively entangled and incredibly expensive for me.
360DigiTMG Artificial intelligence Malaysia

Bhavana said...

It is the expect to give noteworthy information and best takes a shot at, including a perception of the regulatory cycle.
ai course in malaysia

Use an image as your UIBarButtonItem

Using an image as your UIBarButtonItem in your navigationcontroller bar can only be achieved by using a common UIButton as the BarButtonItem...