The lambda expression for C # uses the lambda operator =>, which reads "goes to". The syntax is as follows:

Source: Internet
Author: User

formal parameter list = function Bodythe function body can be enclosed in curly braces for more than one statement. typeYou can assign this expression to a delegate type, as follows:
Delegate int del (int= x=>{return x*x;}; int j = mydelegate (5); // j=25

To create an expression tree type:

using System.Linq.Expressions; // ... Expression <del>=x=>x*x;

the-= = operator has the same precedence as the assignment operator (=) and is the right associative operator. Lambda is used in method-based LINQ queries as parameters for standard query operator methods such as where and where. when you invoke the Where method in the Enumerable class using method-based syntax (as in LINQ to Objects and LINQ to XML), the parameter is the delegate type System.::. func< (Of < (T, tresult>) >). It is most convenient to create a delegate using a LAMBDA expression. For example, when you are in System.Linq.::. When the same method is called in the Queryable class (as in LINQ to SQL), the parameter type is System.Linq.Expressions.::. Expression<func>, where Func is any func delegate that contains up to five input parameters. Similarly, a LAMBDA expression is simply a very concise way to construct an expression tree. Although the types of objects created by Lambda are actually different, Lambda makes the Where call look similar. in the previous example, notice 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 compiler can implicitly convert to the return value of type int. (type inference is discussed in more detail in the following sections.) When the delegate is invoked using 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). Specialthe following rules apply to the range of variables in a LAMBDA expression:the captured variable will not be garbage collected until the delegate referencing the variable goes out of scope. variables introduced within a LAMBDA expression are not visible in the external method. A LAMBDA expression cannot capture a ref or out parameter directly from an enclosing method. a return statement in a LAMBDA expression does not cause the enclosing method to return. A LAMBDA expression cannot contain a goto statement, break statement, or continue statement whose target is outside or inside the body of the containing anonymous function. the essence of a lambda expression is an "anonymous method", that is, when compiling our program code, the compiler automatically converts the lambda expression to an anonymous method, as in the following example:
string[] names={"Agen","Balen","coure","Apple"};string[] findnamea=array.findall<string> (Names,Delegate(stringV) {returnV.startswith ("a");});string[] findnameb=array.findall<string> (Names,v=>v.startswith ("a"));

The anti-compilation code for the two FindAll methods above is as follows:

string []findnamea=array.findall<string> (names,delegate(STRINGV) {Returnv. StartsWith ("a");}); string []findnameb=array.findall<string> (names,delegate(STRINGV) {Returnv. StartsWith ("a");});
you can tell that a lambda expression is an equal to an anonymous method, except that writing code using a lambda expression looks more visually appealing, doesn't it?The syntax format for lambda expressions: parameter list = = Statement or statement blockwhere "parameter column" can contain any parameter (corresponding to a delegate), if there are 0 or more parameters in the parameter sequence, you must enclose the parameter column in parentheses, as follows:() = Console.Write ("0 parameters")I = Console.Write ("1 parameters can omit parentheses in the parameter sequence, value: {0}", I)(x, y) = Console.Write ("Contains 2 parameters, value: {0}*{1}", X, y)in a statement or statement block, if there is only one statement, you can enclose it without braces or you must use it as follows:I = Console.Write ("Only one statement")I + = {Console.Write ("Expressions using curly braces");}//Two statements must be curly bracesI + = {i++; Console.Write ("Case of two statements"); }If the statement or statement block has a return value, if only one statement can not write the "return" statement, the compiler will automatically process, otherwise you must add, the following example:a "lambda expression" is an implementation of a delegate, so the following rules must be followed:1) The number of parameters of the lambda expression must be the same as the number of arguments for the "delegate";2) If the argument of "delegate" includes a ref or out modifier, the parameter sequence of the "lambda expression" must also include a modifier;

The lambda expression for C # uses the lambda operator =>, which reads "goes to". The syntax is as follows:

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.