Lambda expressions in C #

Source: Internet
Author: User

Lambda expressions

A Lambda expression is an anonymous function that can contain expressions and statements and can be used to create a delegate or expression directory tree type. Lambda expressions are composed of Input Functions (if any), Lambda operators (=>), and expressions (or statement blocks. The basic format of lambda expressions is as follows.

(Input parameters) => Expression

Input parameters indicates the input parameter and expression indicates the expression. The input parameter (if any) is on the left of the lambda operator, the expression or statement block is on the right of the lambda operator, and the lambda operator (=>) is read as "goes ". The following instanceCodeUse lambda expressions to calculate the product of two numbers.

X => X * X;

 

Lambda expression input parameters

The number of input parameters can be null, one or more. When the input parameter is null, the arc on the left of the lambda expression cannot be omitted. The following example code uses a Lambda expression to display the string "this is a Lambda expression.". The parameter of this Lambda expression is null.

() => Response. Write ("This is a Lambda expression .");

If the number of input parameters of a Lambda expression is 1, the arc of the input parameter can be omitted. The following example uses the lambaa expression to calculate the product of a parameter. Among them, the first Lambda expression input parameters do not use the arc, and the second Lambda expression input parameters use the arc, which are functionally equivalent.

X => X * X;

(X) => X * X; // The two lambda expressions are equivalent.

If the number of input parameters of a Lambda expression is greater than 1, the arc of the input parameters must be separated by commas. The following sample code uses a Lambda expression to calculate the product of two parameters.

(X, y) => X * Y;

 

Lambda expressions

The right side of a Lambda expression can be not only an expression, but also a statement block. The basic form of lambda expressions is as follows.

(Input parameters) =>{ statement ;}

Input parameters indicates the input parameter, and statement indicates the statement block. It generally consists of multiple statements or expressions. The following example code uses a Lambda expression to calculate the product of two numbers.

(X, y) =>{ int result = x * Y; response. Write (result. tosstring ());}

 

Note: The statement block of the lambda expression must be placed between "{" and.

 

 

 

Lambda expressions in the query

The most common usage of lambda expressions is query. The following example code uses a Lambda expression to query elements in the Select Operation and outputs the query results on the webpage. The steps are described as follows.

(1) Create an integer array, ints, which contains 100 elements (0 ~ 99 ).

(2) Call the select operation and use the lambda expression "I => I % 5" in this operation to calculate the remainder of the element divided by 5. The query result is saved as a values variable.

(3) Call the distinct operation to remove the repeated elements in the values variable and save the result as the result variable.

(4) use the foreach statement to output the value of the element in the result variable.

Private void lambdaquery ()

{

// Construct the data source

Int [] ints = new int [100];

For (INT I = 0; I <100; I ++)

Ints [I] = I;

// Query data

VaR values = ints. Select (I => I % 5)

VaR result = values. Distinct ();

// Display query results

Foreach (var v in values)

{

Response. Write (V + "<br/> ");

}

}

Lambda expression Conversion

Since a Lambda expression itself is an anonymous function, each Lambda expression can be converted to its corresponding function. For example, the lambda expression "x => X * X;" can be converted to the following instance code.

Delegate int del (int I );

Del mydelegate = x => X * X;

Int result = mydelegate (10); // result = 100

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.