Lambda Expressions


With the sample implementation of the extension method Where(), which has been shown, you’ve seen that in the parameter that follows the first parameter with the keyword a delegate is required. The Where() extension method from the Sequence class requires the delegate Func<T, bool>, which is declared this way:

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

The generic delegate Func defines generic types for the parameter and the return type. With the class Racer as type A0, and bool as type T, the method that’s invoked by this generic type is defined as follows:

 bool Method(Racer arg0);

The Where() method uses the delegate to invoke the method defined by the delegate for every element in the collection. If the method returns true, the element is added to the returned collection. Returning all racers that are from Brazil can be implemented by an anonymous method. The anonymous method is defined to get a Racer with the parameter and returns a Boolean value.

  racers.Where<Racer>(    delegate (Racer r)    {       return r.Country == "Brazil";    }); 

Lambda expressions are an evolution of anonymous methods. Converting the previous code segment to the following lambda expression produces the same result.

  racers.Where<Racer>(r => r.Country == "Brazil"); 

The first part of the lambda expression is the input parameter of the delegate. The delegate specifies one parameter, which is a Racer in this example. With lambda expressions, it is not necessary to specify the type; the name of the variable is enough. The type is implicitly defined by the delegate declaration. The variable declaration is followed by the lambda operator, =>. The implementation is defined after the lambda operator. The anonymous method required curly brackets with the implementation; this is not the case with lambda expressions. Also, the lambda expression does not have a return statement. The return is inferred.

Tip 

Lambda expressions require a shorter notation than anonymous methods. After you become familiar with the new syntax, you will find lambda that expressions are easier to read.

With lambda expressions, you can also define the parameter type explicitly and implement the statement body explicitly:

  racers.Where<Racer>((Racer r) => {return r.Country == "Brazil";}); 

If the delegate requires multiple input parameters, you can add the parameters within brackets before the lambda operator:

  (x, y) => x * y; 

Of course, this is also possible to do explicitly:

  (int x, int y) => x * y; 

Instead of passing lambda expressions as parameters of a delegate type, you can assign lambda expressions to variables of type delegate. The same construct as before is changed, again by defining the delegate instances racersFromBrazil, orderSelector, and selectRacer that each reference lambda expressions. The delegate variables are used with the extension methods Where(), OrderByDescending(), and Select().

  Func<Racer, bool> racersFromBrazil = r => r.Country == "Brazil"; Func<Racer, int> orderSelector = r => r.Wins; Func<Racer, Racer> selectRacer = r => r; var brazilRacers =       racers.Where<Racer>(racersFromBrazil).             OrderByDescending<Racer, int>(orderSelector).             Select(selectRacer); 




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net