MICHELVANDERVLUGT.NET RSS 2.0
 Thursday, August 09, 2007

In addition to my first post about the new .NET 3.5 language features my second post in this series will be about Lambda Expressions. Lambda Expressions are basically a follow up to Anonymous Methods (in-line delegates), which were introduced in .NET 2.0. The advantage of Lambda Expressions over anonymous methods is that it is a superset of anonymous methods (more functionality) and that it provides a more concise, functional syntax for writing Anonymous Methods (C# 3.0 specification).

Based on the example of my Extension Methods post a foreach method, which takes a generic Action delegate as a parameter, can be created with an anonymous method. To invoke the SaySomething method for every Person object in the list you can write:

_persons.ForEach(delegate(Person p) { p.SaySomething("Hello"); } );

Using the Lambda Expression syntax this same code will be:

_persons.ForEach( p => p.SaySomething("Hello") );

The syntax of param => expression is not only shorter and easier to read (once you are used to it), it is also implicitly typed. The type of p is inferred by the compiler and IntelliSense as a Person object, which is possible because the ForEach method is executed on a list of Person objects. The parameter p can also be explicitly typed by using the syntax (Person p) => p.Say...().

In this case, where the Lambda Expression takes one parameter and doesn't have a return value, the definition of the method is with a parameter of the generic delegate type Action<T>. So in case you want to write a method, e.g. as an IList extension, that takes these kind of Lambda Expressions you can write:

public static void MyForEach<T>(this IList<T> list, Action<T> doFunc)

This method can be invoked exactly the same as the normal ForEach method, with either a delegate or a Lambda Expression. Of course a lot of other great constructions are possible, for example a DoIfMatch method that executes an action when there is a match found. Note that for the match function you use the generic delegate Func<T, R>, where T is the type of the input parameter and R is the type of the result:

public static void DoIfMatch<T>(this IList<T> list, Func<T, bool> matchFunc, Action<T> doFunc)

This method can be called using:

_persons.DoIfMatch( p => p.Name == "Person II", p => p.SaySomething("Hello") );

In relation to LINQ Lambda Expressions are used in the Where-part and the OrderBy-part. There is a distinction however in LINQ implementations based on IEnumerable and IQueryable. The first one uses the Func<T, R> delegates as explained above. The latter one however uses the Expression<Func<T, R>>, which makes evaluation of the Lambda Expression possible. This way LINQ to SQL, for example, can add the where part in de SQL query before it is send to the database. The Lambda Expression that you use in both cases aren't any different though.

The source code can be downloaded from here: LambdaExpressionsDemo.zip (,87 KB)

Thursday, August 09, 2007 7:15:43 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] -

Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Categories
Archive
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Michel van der Vlugt
Sign In
All Content © 2008, Michel van der Vlugt
DasBlog theme 'Business' created by Christoph De Baene (delarou)