In the latest version of the .NET languages, you can write simple functions that may or may not be named, execute in-line, and return a single value. These functions exist inside your methods and not as separate, stand-alone functions. These functions are called lambda expressions. It’s useful to understand lambda expressions because they are used behind the scenes in LINQ queries. However, they are also valid outside of LINQ.

Let’s take a look at an example. Suppose you want to create a simple function that converts a temperature from Fahrenheit to Celsius. You could do so within your Visual Basic code by first using the keyword Function. Next, you can indicate parameters to that function (in this case, the Fahrenheit value). Lastly, you write an expression that evaluates to a value that can be returned from the lambda expression. The syntax is as follows:

VB
Dim fahToCel = Function(fahValue As Integer) ((fahValue - 32) / 1.8)

The C# syntax is a bit different. In C#, you must explicitly declare a delegate for use by the compiler when converting your lambda expression. Of course, you declare the delegate at the class-level scope. After you have the delegate, you can write the expression inside your code. To do so, you use the => operator. This operator is read as “goes to.” To the left side of the operator, you indicate the delegate type, a name for the expression, and then an = sign followed by any parameters the expression might take. To the right of the => operator, you put the actual expression. The following shows an example of both the delegate and the expression:

C#
//class-level delegate declaration
delegate float del(float f);
//lambda expression inside a method body
del fahToCel = (float fahValue) => (float)((fahValue - 32) / 1.8);

Notice that in both examples, we assigned the expression to a variable, fahToCel. By doing so, we have created a delegate (explicitly converting to one in C#). We can then call the variable as a delegate and get the results as shown here:

VB
Dim celcius As Single = fahToCel(70)

C#
float celcius = fahToCel(-10);

Alternatively, in Visual Basic, we could have written the function in-line (without assigning it to a variable). As an example, we could have written this:

VB
Console.WriteLine((Function(fahValue As Integer) ((fahValue - 32) / 1.8))(70))

Notice in this last example that the function is declared and then immediately called by passing in the value of 70 at the end of the function.

The C# language has its own quirk too. Here you can write multiple statements inside your lambda expression. You do so by putting them inside curly braces and setting off each statement with a semicolon. The following example has two statements inside the lambda expression. The first creates the new value; the second writes it to a console window. Notice too that the delegate must be of type void in this instance and that you still must call the lambda expression for it to execute.

C#
//class level delegate declaration
delegate void del(float f);
del fahToCel = (float fahValue) => { float f =
(float)((fahValue - 32) / 1.8); Console.WriteLine(f.ToString()); };
fahToCel(70);

Lambda expressions are used in LINQ queries for things such as the Where, Select, and Order by clauses. For example, using LINQ, you can write the following statement:

VB
Dim emps = From emp In db.employees
Where(emp.Location = “Redmond”)
Select emp

C#
var emps = from emp in db.employees
where emp.Location == “Redmond”
select emp;

This LINQ code gets converted to lambda expressions similar to this:

VB
Dim emps = From emp In db.employees
.Where(Function(emp) emp.Location = “Redmond”)
.Select(Function(emp) emp)

C#
var emps = from emp in db.employees
where (emp => emp.Location == “Redmond”
select (emp => emp);

Source of Information : Sams Microsoft Visual Studio 2008 Unleashed

0 comments


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner