Objective
Lambda: Simplifies the use of anonymous delegates, allowing you to make your code more concise and elegant. It is said to be one of the most important features that Microsoft has added since c#1.0.
Introduction to Lambda
lambda operator: All lambda expressions are "=" with the new lambda operator, which can be called, "go" or "become." The operator divides the expression into two parts, the left specifies the input parameter, and the right side is the body of the lambda.
Lambda expression:
1. One parameter: param=>expr
2. Multiple parameters: (param-list) =>expr
These things, remember, here we begin to apply and articulate lambda to make you enjoy it.
Lambda Application Description
To illustrate this technique, I'll take an example and then analyze it slowly. Examples are as follows:
namespaceElaboration lambda{ Public classPerson { Public stringName {Get;Set; } Public intAge {Get;Set; } } classProgram { Public StaticList<person>personslist () {List<Person> persons =NewList<person>(); for(inti =0; I <7; i++) {person P=NewPerson () {Name = i +"son", age =8-I,}; Persons. ADD (P); } returnpersons; } Static voidMain (string[] args) {List<Person> persons =personslist (); Persons= persons. Where (p = p.age >6). ToList ();//a collection of all age>6Person per = persons. Singleordefault (p = p.age = =1);//single people class for age=1persons = persons. Where (p = p.name.contains ("son")). ToList ();//all name contains the collection of the son's person } }}
Look at the above example, I believe you can see that it is really a chocolate, hehe, next we look at (p=>p.age>6) such expressions, what is going on.
First, let's look at the commission.
//Entrust a supermarket shopping Delegate intGuangchaoshi (inta); Static voidMain (string[] args) {Guangchaoshi GWL=Jiezhang; Console.WriteLine (GWL (Ten) +"");//Print 20, the application of the delegateConsole.readkey (); } //Checkout Public Static intJiezhang (inta) {returnA +Ten; }
Look at the expression again.
//Entrust a supermarket shopping Delegate intGuangchaoshi (inta); Static voidMain (string[] args) { //Guangchaoshi gwl = Jiezhang;Guangchaoshi GWL = p + p +Ten; Console.WriteLine (GWL (Ten) +"");//Printing 20, application of expressionsConsole.readkey (); }
Delegate with the expression of the two-segment code, we can see some things: in fact, the expression (p = p + 10; p represents a parameter in the delegate method, and the p+10 to the right of the expression symbol is the return result in the delegate method. The warrior detour, the shrimp understand.
Here are two slightly more complex understanding.
1. Multi-parameter
// consignment supermarket delegate int Guangchaoshi (int A , int b); static void Main (string [] args) {Guangchaoshi gwl = (p,z) + = Z (p + 10 ); Console.WriteLine (GWL ( 10 , 100 ) + ); // print 80,z corresponding parameter b,p corresponding parameter a Console.readkey (); }
2. Lambda Body Operation Complex
///<summary>///Entrust a supermarket shopping///</summary>///<param name= "a" >Spend</param>///<param name= "B" >Pay money</param>///<returns>Change</returns>Delegateint Guangchaoshi (int A,Intb);Staticvoid Main (String[] args) {Guangchaoshi GWL = (p, z) => {int Zuidixiaofei = 10if (P < Zuidixiaofei) { return 100else {return z-p-10< Span style= "color: #000000;" >; } }; Console.WriteLine (GWL (100) + ""); // print 80,z corresponding parameter b,p corresponding parameter A
With these examples above, I would like to introduce a system-specific fun<t> delegate below.
T is a parameter type, which is a generic type of delegate that is handy to use.
The first example
Static void Main (string[] args) { Func<intstringten" --return type is string"; Console.WriteLine (GWL (""); // print ' 20--return type is String ', Z corresponds to parameter b,p a Console.readkey (); }
Note: We can see that p here is an int type parameter, whereas the lambda body returns a string type.
And then the last example
Static voidMain (string[] args) {Func<int,int,BOOL> GWL = (p, j) = = { if(p + j = =Ten) { return true; } return false; }; Console.WriteLine (GWL (5,5) +"");//print ' True ', z corresponds to parameter b,p aConsole.readkey (); }
Note: From this example, we can see that p is of type int, j is an int type and the return value is type bool.
After reading the above two examples, I believe you should understand the use of func<t>: multiple parameters, the previous argument for the delegate method, the last parameter, the return type of the delegate method.
Dynamic Creation Method for lambda expression tree
Static voidMain (string[] args) { //i*j+w*xParameterExpression A = Expression.parameter (typeof(int),"I");//Create a parameter in an expression tree, as a node, this is the lowest nodeParameterExpression B = Expression.parameter (typeof(int),"J"); Binaryexpression be= Expression.multiply (A, b);//here i*j, generate a node in the expression tree, one level higher than the above nodeparameterexpression C= Expression.parameter (typeof(int),"W"); ParameterExpression D= Expression.parameter (typeof(int),"x"); Binaryexpression BE1=expression.multiply (c, D); Binaryexpression su= Expression.add (BE,BE1);//operation of two intermediate nodes, generating endpointsExpression<Func<int,int,int,int,int>> lambda = expression.lambda<func<int,int,int,int,int>>(SU,A,B,C,D); Console.WriteLine (Lambda+"");//print ' (i,j,w,x) = ((i*j) + (w*x)) ', z corresponding parameter b,p corresponding parameter aFunc<int,int,int,int,int> f= lambda.compile ();//The lambda expression described by the expression tree is compiled into executable code and the delegate of the lambda expression is generated;Console.WriteLine (F (1,1,1,1) +"");//Printing 2Console.readkey (); }
This piece of code, put it up, carefully understand, understand, lambda expression is basically nothing. Oh..
I'll draw a picture as the end, to make it easy to understand.
Lambda expression tree for the previous segment of code, figure.
Lambda expressions in a detailed