Introduction:
"Lambda expression" (lambda expression) is an anonymous function that introduces lambda expressions in C #3.0. It is a simplified method for anonymous functions and can contain expressions and statements, it can also be used to create a delegate or expression directory tree type.
How to create
Format: (parameter list) =>{ function body}
To create a Lambda expression, specify the input parameter on the left side of the Lambda operator => (if any), and then enter the expression or statement block on the other side. For example, lambda expression x => x * x specifies the parameter named x and returns the square value of x. As shown in the following exampleThe expression is assigned to the delegate type: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpha + placement = "brush: java;"> 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 braces for more than one statement.
The above function body can also be written
del myDelegate = x => { return x * x;};
Use Lambda expressions
=> The operator has the same priority as the value assignment operator (=) and is a right combination operation.
Parentheses are optional only when lambda has only one input parameter; otherwise, parentheses are required. Two or more input parameters in parentheses are separated by commas:
(X, y) => x = y
Use Lamdba statements
The statement lambda is similar to the expression lambda expression, but the statement is enclosed in braces:
delegatevoid TestDelegate(string s);…TestDelegatemyDel = n => { string s = n + " " + "World";Console.WriteLine(s); }; myDel("Hello");
How to Use Lamdba in code
Lambda is used as a parameter for standard query operators (such as Where) in method-based LINQ queries. The parameter is the delegate type System. Func . Using Lambda expressions to create a delegate is the most convenient.
View instances
public delegate TResult Func
(TArg0 arg0)
The delegate can be instantiated as Func. 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 Define a delegate that contains two input parameters (int and string) and returns the bool type. When the following Func delegate is called, the delegate returns true or false to indicate whether the input parameter is equal to 5:
Func
myFunc = x => x == 5;boolresult = myFunc(4); // returns false of course
// A small example of public CommodityCollection Load (string type) {return this. load (p => {p. appendItem ("Code", type );});}
The load method in return is actuallyDelegation with generics, P implementsSqlClauseBuilderUW Method
[Serializable]public abstract class SqlClauseBuilderUW : SqlClauseBuilderIUW{protected SqlClauseBuilderUW();public SqlClauseBuilderUW AppendItem
(string dataField, T data, string op, string template);public SqlClauseBuilderUW AppendItem
(string dataField, T data, string op, string template, bool isExpression);protected override SqlClauseBuilderItemBase CreateBuilderItem();}
public TCollection Load(Action
whereAction);
NamespaceSystem {// Abstract: // encapsulate a method. This method has only one parameter and does not return a value. //// Parameter: // obj: // parameters of the method encapsulated by this delegate. //// Type parameter: // T: // parameter type of the method encapsulated by this delegate. Public delegate void Action
(T obj );}
Summary:
The comprehensive application shows that Lamdba can simplify the query method, and is used in combination with generic delegation and enumeration where methods in the use of Linq. The query method is much simpler, but the internal structure is the same as that of anonymous functions. I have just been familiar with it. Please understand the shortcomings.