Introduced:
the Lambda expression " (lambda expression) is an anonymous function, in the C # 3.0 introduced in the Lambda expression, which is a simplification of anonymous functions that can include expressions and statements, and can be used to create a commit or expression folder tree type.
How to create
format: (Form a list) = = {function Body}
to createLambdaexpression, you need toLambdaoperator=The left side specifies the input parameters (dummy), and then a side input expression or block of statements. For example,LambdaAn expressionx = x * xrefers to the namingxthe number of references and returnsxthe value of the square. as seen in the demo sample below, you can assign this expression to a commit type:
delegate int del (int i); static void Main (string[] args) { del mydelegate = x = x *x; Int J = MyDelegate (5); j = 25}
Note : The function body can be enclosed in curly braces for more than one statement.
The above function body can also be written as
Del MyDelegate = x = = {return x * x;};
Use Lambda An expression
the = = operator has an assignment operator (=) Same priority and right-associative operation
only if The parentheses are optional when lambda has only one input parameter, otherwise the parentheses are required. Two or more of the other input parameters within the parentheses are delimited with commas:
(x, y) = x = = y
Use LAMDBA Statement
Statement Lambda is similar to an expression lambda expression, where only statements are enclosed in curly braces:
Delegatevoid testdelegate (string s); Testdelegatemydel = n = {string s = n + "" + "world"; Console.WriteLine (s); }; Mydel ("Hello");
How to use the code LAMDBA
Lambda is used as a reference in a method-based LINQ query as a standard query operator method, such as where . The number of references is the type of system.func<t, tresult>. It is most convenient to create this trust using a LAMBDA expression .
See Example
Public delegate TResult func<targ0, tresult> (TArg0 arg0)
the ability to instantiate a commit as func<int,bool> MyFunc, where int is the input parameter and bool is the return value. The return value is always specified in the last type parameter. Func<int, String, bool> definition consists of two input parameters (int and string) and returns a delegate of type bool. When the following Func commit is called, the Trust returns TRUE or false to indicate whether the input parameter equals 5:
func<int,bool> MyFunc = x = x = = = 5;boolresult = MyFunc (4); Returns false of course
A sample public commoditycollection Load (string type) {return this is used in the project . Load (p = = { p.appenditem ("Code", type); }); }
the Load method in return is actually a generic commit , and P implements the sqlclausebuilderuw method for inheritance.
[Serializable]public abstract class sqlclausebuilderuw:sqlclausebuilderiuw{protected Sqlclausebuilderuw ();p ublic Sqlclausebuilderuw appenditem<t> (String DataField, T data, String op, String template);p ublic Sqlclausebuilderuw Appenditem<t> (String DataField, T data, String op, String template, bool isexpression);p rotected override Sqlclause Builderitembase Createbuilderitem ();}
Public tcollection Load (action<wheresqlclausebuilder> whereaction);
namespacesystem{//Abstract:// encapsulates a method that has only one parameter and does not return a value. Number of references://obj://The number of the methods that this trust encapsulates. Type parameters:// t://The type of the method that this trust encapsulates. public delegate void Action<in t> (T obj);
Summary:
comprehensive application can be seen LAMDBA methods to simplify the query, and to Linq the use of multi-and generic-entrusted, enumerated where method is used together. Is that the query method simplifies a lot, but the internal structure is the same as the anonymous function. is also just contact understanding, deficiencies also please understand.