For those who have never touched the lambda expression, this thing is very painful, egg pain grammar, egg pain appearance, but like people, if it is too prominent (ugly), general will have big, after all, God is fair!
Before you specifically analyze lambda, say two sentences:
The 1.LAMBDA expression is not necessary because it is an anonymous function that can be done with a lambda expression (and, of course, we also know that anonymous functions are not required).
2.lambda expressions can greatly simplify our use of anonymous functions.
3. Before you learn lambda expressions, it is best to understand the concepts of anonymous functions, delegates, and generics.
MSDN Definition: A LAMBDA expression is an anonymous function that you can use to create a delegate or an expression tree type.
Through the above definition we can understand:
The 1.LAMBDA expression is an anonymous function, but it looks less like an evolutionary version.
The 2.LAMBDA expression is an anonymous function used to create a delegate or a directory tree
The directory tree is who also, put in other chapters, said here only lambda expression creates a delegate.
First, create a delegate
delegate int del (int arg);
Then create a method that conforms to this delegate definition
static int Func1 (int numb) { return numb + ten; }
Create an instance of the Delegate del, where we use the most basic method of creating the delegate.
static void Main (string[] args) { del D1 = new del (FUNC1); Console.WriteLine (D1 (Ten)); Console.readkey (); }
Well, we've created an instance of the delegate del above.
We know that if a method is likely to be used only once, we do not need to create this method alone, but we can use anonymous functions. So let's assume that Func is the one-time-only function, and we can change the code for the instance that created the delegate above to use the version of the anonymous function.
static void Main (string[] args) { del D1 = delegate (int numb) { return numb +; }; Console.WriteLine (D1 (Ten)); Console.readkey (); }
Analysis of this anonymous function: Delegaet keyword, the parameters of the anonymous function type and parameter name, function body, can it be simpler? Keep looking, the main character is coming.
static void Main (string[] args) { del D1 = p = = p + Ten; Console.WriteLine (D1 (Ten)); Console.readkey (); }
P=>p+10 is the lambda expression, let's analyze it, it's simple.
1.=> read as "become", "turn into", read English as "goes to"
2.=> the left p represents the arguments passed to the method when the method is called
3.=> to the right of the p+10, indicating the method body
Compare the above using the anonymous function to create a delegate instance, the left P is equivalent to numb, the right p+10 is equivalent to {return p+10;}
We can be optimistic that the lambda expression is still converted to the corresponding anonymous function during the compilation phase, and don't ask me how I know, I guess!
Draw a picture to show it.
So as we said earlier, lambda expressions are also anonymous functions, but they simplify the use of anonymous functions.
So we said earlier that lambda expressions are used to create delegates, do you think we just created an instance of the delegate again?!
So until now, you should remember one thing: Lamb expressions are nothing special, just a language feature of C #, or a syntax.
The truth is clear, let's step up a little bit.
A delegate that defines two parameters
delegate int del (int arg1,int arg2);
Try it yourself, first create an instance of the delegate using an anonymous function, and then look at it using a lambda expression.
Using anonymous functions
del D1 = delegate (int numb1, int numb2) { return numb1 + numb2; }; Console.WriteLine (D1 (10,10)); Console.readkey ();
Using lambda expressions
del D1 = (p, z) = = p + z; Console.WriteLine (D1 (10,10)); Console.readkey ();
Yes, two parameters when the time is to use parentheses around, with a good division of two parameters, where p means that numb1,z represents NUMB2.
Let's get a little more complicated: Define a shopping delegate
<summary>/// Define a shopping delegate, accept two parameters respectively///</summary>// <param name= "spend" > Actual consumption amount </param>// <param name= "Cash" > Amount paid to Merchant </param>// <returns> merchant recovered amount </ Returns> Delegate Double shoppingdelegate (double spend, double cash);
An instance of this delegate is created using an anonymous method, and the logic of the method is slightly more complex
Shoppingdelegate D1 = delegate (double spend, double cash) {/ * variable card represents the amount on the membership card, we have experience, if often will be a few cents deposited into the membership card, convenient next use, This will bring less change. If the amount of the membership card is >0.5, the amount on the membership card will be used, otherwise not */ double card = 0.5; if (Card > 3) { return cash-spend + card; } else { return cash-spend; } };
So how do you change to a lambda expression? Yes, lambda expressions also use {} If the code is more numerous.
Shoppingdelegate D1 = (p,z) and {/ * variable card represents the amount on the membership card, we have experience, if a few cents will be credited to the membership card, convenient next use, so you can take less change. If the amount of the membership card is >0.5, the amount on the membership card will be used, otherwise not */ double card = 0.5; if (Card > 3) { return z-p + card; } else { return z-p; } };
Some people ask, why are the parameters P and z in the lambda expression not represented by spend and cash? Of course, but the lambda expression comparison, can be expressed in a character, absolutely do not use a half.
OK, here's the section, the next section is devoted to the use of lambda Expressions with LINQ.
Lambda expressions in a detailed