Lambda expression lambda expression (C # Programming Guide)

Source: Internet
Author: User
Tags anonymous

Http://msdn.microsoft.com/zh-cn/library/bb397687.aspx


A LAMBDA expression is an anonymous function that can be used to create a delegate or an expression tree type. By using a lambda expression, you can write a local function that can be passed as a parameter or returned as a function call value. Lambda expressions are especially useful for writing LINQ query expressions.

To create a lambda expression, you need to specify an input parameter (if any) on the left side of the lambda operator =>, and then enter an expression or statement block on the other side. For example, the lambda expression x => x * x refers to an argument named X and returns the square value of x. As shown in the following example, you can assign this expression to a delegate type: C #

delegate int del (int i);
static void Main (string[] args)
{
    del mydelegate = x => x * x;
    Int J = MyDelegate (5); j =
}

To create an expression tree type: C #

Using System.Linq.Expressions;

Namespace ConsoleApplication1
{
    class program
    {
        static void Main (string[] args)
        {
            expression<del> Myet = x => x * x;
        }
    }

The => operator has the same precedence as the assignment operator (=) and is a right associative operation (see the "Binding" section of the "operator" article).

A parameter used by a LAMBDA in a method-based LINQ query as a standard query operator method, such as a where.

When invoking the Where method in the enumerable class using the method based syntax (as in LINQ to Objects and LINQ to XML), the parameter is the delegate type System.func<t, tresult>. It is most convenient to use a LAMBDA expression to create this delegate. For example, when you call the same method in the System.Linq.Queryable class (as in Linq to SQL), the parameter type is SYSTEM.LINQ.EXPRESSIONS.EXPRESSION<FUNC> Func is any Func delegate with up to 16 input parameters. Similarly, Lambda expressions are simply a very concise way to construct the expression tree. Although the objects created by the lambda actually have different types, the lambda makes the Where call look similar.

In the previous example, note that the delegate signature has an implicit type input parameter of type int and returns an int. You can convert a LAMBDA expression to a delegate of that type, because the expression also has an input parameter (x), and a return value that the compiler can implicitly convert to type int. (type inference is discussed in detail in the following sections.) When a delegate is invoked with input parameter 5, it returns the result 25.

Lambda is not allowed on the left side of the IS or as operator.

All restrictions that apply to anonymous methods also apply to LAMBDA expressions. For more information, see Anonymous Methods (C # Programming Guide). Expression Lambda

The lambda expression on the right side of the => operator is called an expression lambda. Expression Lambda is widely used in the construction of Expression trees (C # and Visual Basic). The expression lambda returns the result of an expression and takes the following basic form:

(input parameters) => expression

Parentheses are optional only if the lambda has only one input parameter, otherwise the parentheses are required. Two or more input parameters in parentheses are separated by commas: C #

(x, y) => x = = y

Sometimes, the compiler is difficult or unable to infer the input type. If this happens, you can explicitly specify the type as shown in the following example: C #

(int x, string s) => s.length > x

Specify 0 input parameters using an empty bracket: C #

() => SomeMethod ()

In the previous example, note that the body of an expression Lambda can contain a method call. However, if you want to create an expression tree that is evaluated outside of the. NET Framework (for example, in SQL Server), you should not use method calls in lambda expressions. Outside of the. NET common language Runtime context, methods do not make any sense. Statement Lambda

The statement lambda is similar to an expression lambda expression, except that the statement is enclosed in curly braces:

(input parameters) => {statement;}

The body of a statement lambda can contain any number of statements, but there is usually no more than two or three. C#

delegate void Testdelegate (string s);
... Testdelegate Mydel = n => {string s = n + "" + "world"; Console.WriteLine (s); };
Mydel ("Hello");

Like anonymous methods, statement lambda cannot be used to create an expression tree. Asynchronous lambda

By using the async and await keywords, you can easily create lambda expressions and statements that contain asynchronous processing. For example, the following Windows forms example contains an event handler that invokes and waits for an asynchronous method Examplemethodasync. C#

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.