Extension Methods
Chinese Version of Enterprise Services with the .NET Framework

Lambda Expressions and Anonymous Methods

C# 3.0 enhances anonymous methods that are new with C# 2.0: lambda expressions. To show the concept of anonymous methods and lambda expressions let's look at this simple extension method DisplaySelected that writes selected properties to the console. Because the method is defined as a generic method, it can work with every IEnumerable<T> collection. The implementation includes a simple foreach statement where every element from the source collection is written to the console. What information from the element is written to the console is defined by the selector.

   public static IEnumerable<T> DisplaySelected<T, TSelector>(
        
this IEnumerable<T> source, Func<T, TSelector> selector)
   {
      foreach (T element in source)
      {
         Console.WriteLine(selector(element));
      }
      return source;
   }

The second parameter of the extension method is selector of type Func<T, TSelector>. Func is defined as this delegate:

public delegate T Func<A0, T>(A0 arg0);

To first demonstrate invoking the method DisplaySelected, I'm defining the method GetFirstname where the type T for the Func delegate. GetFirstname returns the firstname of the passed object, so that if used with DisplaySelected, the firstname of a person is written to the console.

static string GetFirstname(Person p)
{
   return p.Firstname;
}

Now we have different ways to invoke the method DisplaySelected.

persons is an object of type List<Person>.

A traditional way to invoke the method DisplaySelected is by instantiating the delegate Func, and passing the address of GetFirstname:

   persons.DisplaySelected<Person, string>(new Func<Person, string>(GetFirstname));

C# 2.0 allows inferring the delegate. So instead of instantiating a new delegate Func, the method name can be directly passed to DisplaySelected. The compiler creates the same code to instantiate the delegate.

persons.DisplaySelected<Person, string>(GetFirstname);

Instead of using the method GetFirstname, C# 2.0 allows a anonymous method syntax where the implementation of the method that is invoked by the Func delegate can be directly written as the parameter.

persons.DisplaySelected<Person, string>(delegate(Person p) {return p.firstname;});

C# 3.0 allows a new syntax for lambda expressions. Lambda expressions are written as a parameter list (p), followed by the => token and an expression or statement block.

persons.DisplaySelected<Person, string>(p => p.firstname);

Because the generic types of DisplaySelected can be inferred that way, the method call can also be written in this simple form: 

persons.DisplaySelected(p => p.firstname);

Lambda expressions offer great possibilities :-)

Christian

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Your Information

(Name and email address are required. Email address will not be displayed with the comment.)