Thursday 29 December 2011

C# Lambda Expression

You want to use lambda expressions in the C# programming language, allowing functions to be used as data such as variables or fields. The lambda expression syntax uses the => operator and this specifies how the parameters and statement body of the anonymous function instance are separated, and provides a formal name to the environment variables. Here we look at lambda expressions.Lambda expression syntax
Tip: Lambda expressions use the token => in an expression context.

Example

This program demonstrates how you can insert lambda expressions and other anonymous functions into a C# method. The source text uses the => operator, which is a syntax for separating the parameters to a method from the statements in the method's body.
The => operator can be read as "goes to" and it is always used when declaring a lambda expression. In programming languages, a lambda expression allows you to use a function with executable statements as a parameter, variable or field.
Program that uses lambda expressions [C#]

using System;

class Program
{
    static void Main()
    {
 //
 // Use implicitly typed lambda expression.
 // ... Assign it to a Func instance.
 //
 Func<int, int> func1 = x => x + 1;
 //
 // Use lambda expression with statement body.
 //
 Func<int, int> func2 = x => { return x + 1; };
 //
 // Use formal parameters with expression body.
 //
 Func<int, int> func3 = (int x) => x + 1;
 //
 // Use parameters with a statement body.
 //
 Func<int, int> func4 = (int x) => { return x + 1; };
 //
 // Use multiple parameters.
 //
 Func<int, int, int> func5 = (x, y) => x * y;
 //
 // Use no parameters in a lambda expression.
 //
 Action func6 = () => Console.WriteLine();
 //
 // Use delegate method expression.
 //
 Func<int, int> func7 = delegate(int x) { return x + 1; };
 //
 // Use delegate expression with no parameter list.
 //
 Func<int> func8 = delegate { return 1 + 1; };
 //
 // Invoke each of the lambda expressions and delegates we created.
 // ... The methods above are executed.
 //
 Console.WriteLine(func1.Invoke(1));
 Console.WriteLine(func2.Invoke(1));
 Console.WriteLine(func3.Invoke(1));
 Console.WriteLine(func4.Invoke(1));
 Console.WriteLine(func5.Invoke(2, 2));
 func6.Invoke();
 Console.WriteLine(func7.Invoke(1));
 Console.WriteLine(func8.Invoke());
    }
}

Output

2
2
2
2
4

2
2
Syntax. In many of the statements in the example, you will see the => syntax, which can be read as "goes to" and it separates the arguments from the method body of a lambda expression. It is not a comparison operator. The => syntax can separate an empty parameter list, a formal parameter list or an implicit parameter list from the the body. The right side of the lambda expression can be a statement list inside curly brackets with a return statement, or an expression.
The C# programming languageOverview. The instances with identifiers func1 through func8 all denote anonymous function instances in the C# language. The Func<TResult> type indicates an anonymous function with one result value and no parameter. The Func<T, TResult> type indicates a function with one parameter and one result value. The Action type indicates a function instance that does not receive a parameter and does not return a value.
Delegate keyword. The program also demonstrates how the delegate keyword can be used to denote an anonymous function in the C# language. After the delegate keyword, you can use a formal parameter list or even omit the list if there are no parameters. Afterwards, you must specify a statement list that is executed when the anonymous function is invoked.
Invoking Func and Action instances. Finally, the program demonstrates how you can call the methods that were all specified inside the method body. The Func generic type and the Action type have an instance method called Invoke. It receives a number of parameters that depends on the type. The method body of each of the lambdas and anonymous functions is then executed.

Anonymous functions

The term anonymous function describes both delegates and lambda syntaxes in the C# language. The key part of an anonymous function is that it does not have a name and cannot be invoked with normal method lookup. For this reason, method overloading is not possible for anonymous functions.
Many experts regard lambda expressions as a complete improvement over the delegate syntax. It is sometimes advised that you use lambda expressions instead of the delegate keyword syntax when it is possible.

Specification

The C# language specification itself describes the anonymous function types in the language. The annotated edition of The C# Programming Language (3rd Edition) covers all the syntaxes available, and this article derives its example from the specification's. You can find more detail on this topic using the precise technical terminology on page 314 of this book.

Summary

We looked at the syntactic rules of lambda expressions in the C# programming language with help from the C# specification itself. We described lambda expressions with zero arguments, one argument, two arguments, and one return value. We also looked at the delegate keyword when used for anonymous functions. Finally, we explored the Func generic type in the base class library as well as the Action type, and the Invoke instance methods on these types.

No comments:

Post a Comment

Total Pageviews