1, the naïve hot, the programmer is not easy to live, Sunday, also to top the burning sun, summed up these things.
2, boast Lambda bar: Simplify the use of anonymous delegate, let you make the code more concise, 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{PublicClassperson {PublicString Name {GetSet; }Publicint Age {GetSet; } }Classprogram {PublicStatic list<person>Personslist () {list<person> persons =New list<person>();for (int i =0; I <7; i++) {Person p =New Person () {Name = i +"Son", age =8-I,}; Persons. ADD (P); }ReturnPersons }static void Main ( String[] args) {list<person> persons = Personslist (); persons = persons. Where (p = p.age > 6). ToList (); //1); //age=1 a single people class persons = 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 shoppingDelegateint Guangchaoshi (Int a); static void Main ( String[] args) {Guangchaoshi gwl = Jiezhang; Console.WriteLine (GWL ("); // print 20, the application of the delegate // checkout public static int Jiezhang (int a) {return A + 10; }
Look at the expression again.
commissioned shopping mall int Guangchaoshi (intvoid Main (string//) Console.WriteLine (GWL (// print 20, expression application
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
commissioned shopping mall int Guangchaoshi (intA,intvoid Main (string); Console.WriteLine (Gwl,// 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.
func<t> Delegate
T is a parameter type, which is a generic type of delegate that is handy to use.
The first example
void Main (string[] args) { func<"--return type is string"; Console.WriteLine (GWL (/// print ' 20--return type is String ', Z corresponds to parameter b,p corresponding parameter 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
Staticvoid Main ( string[] args) {func<int, int, bool> gwl = (p, j) => {if (p + j = 10return truereturn false;}; Console.WriteLine (GWL (5,5) + // print ' True ', z corresponding parameter b,p corresponding parameter a Console.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
Staticvoid Main (String[] args) {//I*j+w*x parameterexpression a = Expression.parameter (typeofint),"I");//Create a parameter in an expression tree, as a node, here is the lowest node parameterexpression b = Expression.parameter (typeofint),"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 (typeofint),"W"); ParameterExpression d = Expression.parameter (typeofint),"X"); Binaryexpression BE1 =Expression.multiply (c, D); Binaryexpression su = Expression.add (BE,BE1);//Operation of two intermediate nodes, generating endpointsexpression<func<IntIntIntIntInt>> lambda = expression.lambda<func<IntIntIntIntInt>>(SU,A,B,C,D); Console.WriteLine (Lambda +// print ' (i,j,w,x) = ((i*j) + (w*x)) ', z corresponding parameter b,p corresponding parameter a< Span style= "color: #000000;" > Func<int, int, int , int, int> f= lambda.compile (); // The lambda expression described by the expression tree, compiled into executable code, and the delegate that generated the lambda expression; Span style= "color: #000000;" > Console.WriteLine (f (1, 1, 1, // print 2 Console.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 expression detailed "go"