Lambda Expressions

C# 3.0 extends the concept of anonymous methods and introduces lambda expressions, which are more powerful and flexible than anonymous methods.

Tip

Lambda expressions get their name from lambda calculus, which is a complicated topic, but in a nutshell, it’s a mathematical notation for describing functions. That’s pretty much what’s going on here; lambda expressions describe methods without naming them.

You define a lambda expression using this syntax:

(input parameters) => {expression or statement block};

The lambda operator => is newly introduced in C# 3.0 and is read as "goes to”. The left operand is a list of zero or more input parameters, and the right operand is the body of the lambda expression. Notice that => is an operator, which means that the preceding line of code is an expression. Just as x + x is an expression that returns a value—if x is 2, the expression returns the int value 4—a lambda expression is an expression that returns a method. It’s not a method by itself. It’s tricky to think of expressions as returning a method instead of a value, but at the beginning of this chapter, you wouldn’t have thought of passing a method as a parameter, either.

You can thus rewrite the delegate definition as follows:

theClock.OnSecondChange +=
    (aClock, ti) =>
    {
        Console.WriteLine("Current Time: {0}:{1}:{2}",
                           ti.hour.ToString( ),
                           ti.minute.ToString( ),
                           ti.second.ToString( ));
    };

You read this as "theClock’s OnSecondChange delegate adds an anonymous delegate defined by this lambda expression.” The two parameters, aClock and ti, go to the WriteLine expression that writes out the hour and minute and second from ti.

The two input parameters, aClock and ti, are of type Clock and TimeInfoEventArgs, respectively. You don’t need to specify their types, because the C# compiler infers their type from the OnSecondChange delegate definition. If the compiler is unable to infer the type of your operands, you may specify them explicitly, like this:

(Clock aClock, TimeInfoEventArgs ti) => {...};

You might also want to specify the types of the input parameters to make the code more readable.

If your method doesn’t have any input parameters, you write a pair of empty parentheses, like this:

( ) => {Console.WriteLine("No parameters here."}};

If you have only one input parameter, you can skip the parentheses:

n => {n * n};

Finally, if your method has only one statement, you can skip the braces as well:

n => n * n

So, what’s the difference between lambda expressions and anonymous methods? Anonymous methods were introduced in C# 2.0 specifically to deal with situations where you didn’t want to define a method for a delegate; that’s why anonymous methods use the delegate keyword, and can be used only in the context of delegates. Lambda expressions were introduced in C# 3.0 to take that idea further. Specifically, lambda expressions were introduced to work with LINQ, the Language Integrated Query, which has to do with handling data. You’ll see more about LINQ in Chapter 21. For now, you can use lambda expressions anywhere you’d want to use an anonymous method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset