Lambda expressions, lambda expressions

Source: Internet
Author: User

Lambda expressions, lambda expressions

1. A Lambda expression is an anonymous function. Its essence is actually a function. Therefore, any Lambda expression can be implemented or replaced by a common function in other ways.

2. Lambda expression cloud OPERATOR: =>, which is read as "goes ".

3. Lambda Syntax: parameter list => function body. There are some differences in the form according to the usage, as shown below:

1>. If there is only one parameter, "()" in the parameter list can be omitted. If there is no or more parameters, no;

Num => Console. Write ("the brackets in the parameter list can be omitted when one parameter is set. Value: {0}", num );

() => Console. Write ("0 Parameters ");

(X, y) => Console. Write ("contains two parameters, value: {0}, {1}", x, y );

2> when a Lambda expression is directly used as a delegate implementation method, the type before the parameters in the form parameter list can sometimes be omitted. The ref and out modifiers must correspond to the delegate;

3>. If there is only one statement in the function body, the braces can be omitted; otherwise, no;

Num => Console. Write ("only one statement ");

Num => {num + = 1; Console. Write ("two or more statements must have braces ");};

4> when the function body has a return value, if only one statement can omit "return", otherwise it cannot.

4. Func <...> and Action <...>

Both are delegates in the System space in the. net class library. Each delegate can support up to 16 parameters;

Among them, Func <...> is the delegate with the returned value, and Action <...> is the delegate without the returned value;

. In the. net class library and when the delegate is used as a parameter or variable during normal development, Func can be used for 16 parameters or below <...> or Action <...>.

5. Linq

At the beginning, Lambda expressions are always confused with Linq (horizontal comparison of Low );

The Language Integrated Query (Linq) is a set of extensions used for the C # Language. It allows you to write C # code to Query memory data in the same way as a database;

The parameters of the Select, OrderBy, GroupBy, Where, Min, Max, and other methods of Linq are basically Func <...> type delegation. You can use Lambda expressions for definition in specific use.

 

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.