~, not to mention the delegate and lambda expression, first look at two examples:1. With a delegate, add 10 to a number and the following code:
classProgram {Private Delegate int Jisuan(inta); Static voidMain (string[] args) {Jisuan js=Zengshi; Console.WriteLine (JS (Ten)); Console.readkey (); }
Public Static intZengshi (inta) {returnA +Ten; } }2. Using a lambda expression, add 10 to a number with the following code:
class program { private delegate int Jisuan ( Span style= "color: #0000ff;" >int a); static void Main (string [] args) {Jisuan js = P=>p+10 ; Console.WriteLine (JS ( 10
The P in Example 2 (p=>p+10) above is the argument in the example 1 delegate method, and (P+10) is the return value in the example 1 delegate method, where (= =) is the legendary lambda operator. With this explanation, we don't know if you know the connection between a delegate and a lambda expression. Yes, a lambda expression is another way of expressing a delegate, mainly to make the code simple and elegant. Of course, since p is a parameter, then in the case of multiple parameters, to pass multiple arguments, the parameters are written in parentheses, and then separated by commas, such as: (P,Q) =>p+q+10;
Add one point:
The left side of the lambda operator is the passed-in argument, and the right side is the body of the lambda expression, which can be an expression on the right, or a piece of code with a return value. I don't have an example here.
Delegates and lambda expressions