With the evolution of programming languages, we can see a very obvious trend: Let Computers understand programmers, rather than programmers understand computers. Lambda expressions in C #3.0 clearly show this point.
Now let's look at an example:
// Use the anonymous method in C #2.0 to search for "All strings containing Lambda substrings ":
List. findall (
Delegate (string s )...{
Return S. indexof ("Lambda")> = 0 ;}
);
// Use the lambda expression in C #3.0 to find "all strings containing Lambda substrings ":
List. findall (S => S. indexof ("Lambda")> = 0 );
We can see that a new symbol "=>" is used in lambda expressions like "Export" in mathematics. Here we simply call it "launch ". The above means that the string s introduced by S contains the string "Lambda. The type of S is derived from the compiler. This kind of expression is more in line with the human thinking model. It sounds natural that we often use what we launch in mathematics. Lambda expressions are not the first in Microsoft. They are the result of academic research, but Microsoft has introduced them into programming languages.
The Lambda format is as follows:
/** // * (Parameter list) => expression or statement Block
There can be multiple parameters, one parameter, or no parameter. The parameter type can be implicit or explicit. */
// Example:
(X, y) => X * y // multiple parameters, implicit type => Expression
X => X * 10 // single parameter, implicit type => Expression
X =>... {return x * 10;} // single parameter, implicit type => statement Block
(Int x) => X * 10 // single parameter, explicit type => Expression
(Int x) =>... {return x * 10;} // single parameter, explicit type => statement Block
() => Console. writeline () // No Parameters
We can see that Lambda expressions are very flexible. The parameter types can be omitted and can be deduced by the compiler. What is the essence of lambda expressions? Open an assembly with ildasm and check whether it is actually an anonymous method. It's just that the compiler does this for you.
The following example describes how the compiler works:
// Take list. findall (S => S. indexof ("ABC")> 0). In this example, let's see what the compiler generates behind it.
Delegate bool mydelegate1 (string S );
Mydelegate md1 = new mydelegate (XXXXX); // XXXXX indicates the name of the method below.
List. findall (md1 );
Public static bool XXXXX (string s) // Xxxxx is the method name.
...{
Return S => S. indexof ("ABC")> 0;
}
/** // * Is this method quite familiar? Yes, it is in C #1.0. Don't worry about whether it is the anonymous method in C #2.0 or
All lambda expressions in C #3.0 are converted to this form by the compiler */
Let's take a look at a simple example of Lambda:
Delegate int delegate1 (int A, int B );
Delegate double delegate2 (double A, double B );
Delegate string delegate3 (string S1, string S2 (;
Public class lambdaexpression
...{
Public static void dosomething (delegate1 D1 )...{....}
}
// We can use the following method to call dosomething
Lambdaexpression. dosomething (a, B) => A + B); // here, A, B, and '+' can both be replaced with other variable names or other operators.
/** // * Now we can infer that the types of A and B are all int, because the dosomething parameter is delegate1, And the delegate1 requestor
The method parameter is int, so it is natural to export a, B is int type */
// If we write the dosomething method in this way
Public static void dosomething (delegate3 D3 )...{....}
// Then call
Lambdaexpression. dosomething (S1, S2) => S1 + S2 );
// Obviously, S1 and S2 will be exported to the string type.
// As for the more detailed work on the back of the compiler, we have already said it. I will not elaborate on it here.
Lambda expression l can be converted to delegate type D, which must meet the following conditions:
• L and D have the same number of parameters.
• The parameter type of l must be the same as that of D. Note that implicit types must be involved in type discrimination.
• D returns the same type as l, whether expression or block.
As long as the above conditions are met, lambda expressions can be used where delegate expressions can be used.
We can see that Lambda expressions only express anonymous methods in another form, but the meaning is not just that, with the increasing use of lambda expressions, you will feel that the original anonymous method is very stupid, and you will not want to use it again. It is like when people get used to the programming convenience brought by object-oriented language, and no one wants to write Object-oriented Programs in C language.