Lambda expressions

With the C# 3.0 new feature extension method, and the C# 2.0 new feature anonymous method (or inline method), Visual Studio 2008 introduces a new expression called lambda expression.

Lambda expression is actually a syntax change for anonymous methods. It is just a new way of writing anonymous methods.

Firstly, in C# 3.0, there is a new generic delegate type, Func<A,R>, which presents a function taking an argument of type A, and returns a value of type R:

delegate R Func<A,R> (A Arg);

In fact, there are several overloaded versions of Func, of which Func<A,R> is one.

Now, we will use this new generic delegate type to define an extension:

public static IEnumerable<T> Get<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
foreach (T item in source)
{
if (predicate(item))
yield return item;
}
}

This extension method will apply to an object that extends the IEnumerable interface, and has one parameter of type Func, which you can think of as a pointer to a function. This parameter function is the predicate to specify the criteria for the selection. This method will return a list of objects that match the predicate criteria.

Now we can create a new function as the predicate:

public static bool IsVege(Product p)
{
return p.ProductName.Contains("vegetable");
}

Then we can use the extension method Get to retrieve all of the vegetable products, like this:

var veges1 = products.Get(IsVege);

We have now created the products list, with five products, of which two are vegetables. So veges1 is actually of the IEnumerable<Product> type, and should contain two products. We can write the following test statements to print out the results:

Console.WriteLine("
There are {0} vegetables:", veges1.Count());
foreach (Product p in veges1)
{
Console.WriteLine("Product ID: {0} Product name: {1}",
p.ProductID, p.ProductName);
}

The output will be:

Lambda expressions

Or we can first create a new variable of type Func, assign the function pointer of IsVege to this new variable, and then pass this new variable to the Get method like this:

Func<Product, bool> predicate = IsVege;
var veges2 = products.Get(predicate);

Variable veges2 will contain the same products as veges1.

Now, let us use the C# 2.0 anonymous method to rewrite the above statement, which will now become:

var veges3 = products.Get(
delegate (Product p)
{
return p.ProductName.Contains("vegetable");
}
);

At this time, we put the body of the predicate method IsVege inside the extension method call, with the keyword delegate. So, in order to get the vegetables from the products list, we don't have to define a specific predicate method. We can specify the criteria on the spot, when we need it.

The lambda expression comes into play right after the above step. In C# 3.0, with lambda expression, we can actually write the following one line statement to retrieve all of the vegetables from the products list:

var veges4 = products.Get(p => p.ProductName.Contains("vegetable"));

In the above statement, the parameter of the method Get is a lambda expression. The first p is the parameter of the lambda expression, just like the parameter p in the anonymous method when we get veges3. This parameter is implicitly typed and, in this case, is of the type Product, because this expression is applied to a Products object, which contains a list of Product objects. This parameter can also be explicitly typed, like this:

var veges5 = products.Get((Product p) => p.ProductName.Contains("vegetable"));

The parameter is followed by the => token, and then followed by an expression or a statement block, which will be the predicate.

So, now we can easily write the following statement to get all of the candy products:

var candies = products.Get(p => p.ProductName.Contains("candy"));

At compile time, all lambda expressions are translated into anonymous methods according to the lambda expression conversion rules. So, again this feature is only a Visual Studio 2008 feature. We don't need any special .NET runtime library or instructions to run an assembly containing lambda expressions.

In short, lambda expressions are just another way of writing anonymous methods in a more concise, functional syntax.

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

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