[Reprinted from] http://hi.baidu.com/heiru/blog/item/1178788978e400bb0e244417.html
All lambda expressions use the lambda operator =>, which reads "goes ". (Msdn) http://msdn.microsoft.com/zh-cn/library/bb397687.aspx
Lambda expressions (lambda expressions) and anonymous methods are actually one thing. The only difference is that their syntax format is different. Lambda expressions are further evolved in terms of syntax. In essence, they are one thing. Their role is to generate methods. That is, the inline method.
Reference from C # head Architect Anders hejlsberg:
Http://www.ondotnet.com/pub/a/dotnet/2005/10/31/interview-with-anders-hejlsberg-part-2.html? Page = 2
Lambda expressions and anonymous methods are really just two words for the same thing. The only thing that differs is, "What does the syntax look like? "And the lambda expressions are a further evolution of the syntax. But underneath, they do the same thing. They generate methods. You know, they're in-line methods.
Therefore, we should understand both the anonymous method and Lambda expression. Next, let's take a look at a simpleCodeIn this example, the anonymous method and Lambda expression are used to search for arrays respectively:
Use the. NET 2.0 anonymous method to search for the string array containing
Static void main (string [] ARGs)
{
String [] list = new string [] {"ABC", "12", "Java "};
String [] LL = array. findall (list,
Delegate (string S)
{
Return S. indexof ("A")> = 0;
}
);
Foreach (string VaR in LL)
{
Console. writeline (VAR );
}
Console. Readline ();
}
Use a. Net 3.5 Lambda expression to search for a string array containing
Static void main (string [] ARGs)
{
String [] list = new string [] {"ABC", "12", "Java "};
String [] LL = array. findall (list, S => (S. indexof ("A")> = 0 ));
Foreach (string VaR in LL)
{
Console. writeline (VAR );
}
Console. Readline ();
}
From the two examples above, we can see that:
From the perspective of code writing, code readability: lambda expressions are simpler than anonymous methods.
Lambda expressions and anonymous methods both do the same thing. Let's write less function definitions. The function call and implementation are completed together.
Lambda expressions are written in the following format:
(Parameter list) => expression or statement Block
Where:
Number of parameters: there can be multiple parameters, one parameter, or no parameter.
Parameter type: It can be implicitly or explicitly defined.
Expression or statement block: This part is the implementation part (function body) that we usually write functions ).
Examples of writing lambda expressions:
An example of a Lambda expression with two parameters is as follows:
Note: not to mention the complexity. In LINQ, the delegate and dosomething in the following code are actually done for you. Therefore, you only need to write
VaR T = dosomething <int> (7, 8, (x, y) => X * Y); such a row.
Public Delegate t hongjunguotest01 <t> (T T1, t T2 );
Class Program
{
Private Static t dosomething <t> (T T1, t T2, hongjunguotest01 <t> match)
{
Return match (T1, T2 );
}
Static void main (string [] ARGs)
{
VaR T = dosomething <int> (7, 8, (x, y) => X * y );
Console. writeline (t );
Console. Readline ();
}
}
The following statements are also correct (you only need to modify the code in the main function and do not need to change it elsewhere ):
VaR T = dosomething <int> (7, 8, (int x, int y) => X * y );
VaR T = dosomething <string> ("7", "8", (x, y) => X + Y );
Or we can write a more complex statement block: => a block on the right.
VaR T = dosomething <int> (7, 8, (x, y) => {If (x <5) {return (x + 8) * Y ;} else {return y ;}});
The first example is a parameter example. We will not give an example of a parameter. The following is an example without a parameter:
Public Delegate void hongjunguotest02 ();
Class Program
{
Private Static void dosomething (hongjunguotest02 match)
{
Match ();
}
Static void main (string [] ARGs)
{
Dosomething () => console. writeline ("jajaja "));
Console. Readline ();
}
}
Functional Programming
The anonymous method and lambda expressions are the products of functional programming ideas,
Functional programming is obscure from the conceptual point of view, simply put:
Function programming features no variables, allAlgorithmIt is written using recursive functions. WholeProgramThere are no other types of statements except that the defined function is a call function.
The benefits and features of this programming idea,
Lambda expressions (lambda expressions) and anonymous methods are actually one thing. The only difference is that their syntax format is different. Lambda expressions are further evolved in terms of syntax. In essence, they are one thing. Their role is to generate methods. That is, the inline method.
Reference from C # head Architect Anders hejlsberg:
Http://www.ondotnet.com/pub/a/dotnet/2005/10/31/interview-with-anders-hejlsberg-part-2.html? Page = 2
Lambda expressions and anonymous methods are really just two words for the same thing. The only thing that differs is, "What does the syntax look like? "And the lambda expressions are a further evolution of the syntax. But underneath, they do the same thing. They generate methods. You know, they're in-line methods.
Therefore, we should understand both the anonymous method and Lambda expression. Next, let's take a look at a simple code example, using anonymous methods and lambda expressions to search for Arrays:
Use the. NET 2.0 anonymous method to search for the string array containing
Static void main (string [] ARGs)
{
String [] list = new string [] {"ABC", "12", "Java "};
String [] LL = array. findall (list,
Delegate (string S)
{
Return S. indexof ("A")> = 0;
}
);
Foreach (string VaR in LL)
{
Console. writeline (VAR );
}
Console. Readline ();
}
Use a. Net 3.5 Lambda expression to search for a string array containing
Static void main (string [] ARGs)
{
String [] list = new string [] {"ABC", "12", "Java "};
String [] LL = array. findall (list, S => (S. indexof ("A")> = 0 ));
Foreach (string VaR in LL)
{
Console. writeline (VAR );
}
Console. Readline ();
}
From the two examples above, we can see that:
From the perspective of code writing, code readability: lambda expressions are simpler than anonymous methods.
Lambda expressions and anonymous methods both do the same thing. Let's write less function definitions. The function call and implementation are completed together.
Lambda expressions are written in the following format:
(Parameter list) => expression or statement Block
Where:
Number of parameters: there can be multiple parameters, one parameter, or no parameter.
Parameter type: It can be implicitly or explicitly defined.
Expression or statement block: This part is the implementation part (function body) that we usually write functions ).
Examples of writing lambda expressions:
An example of a Lambda expression with two parameters is as follows:
Note: not to mention the complexity. In LINQ, the delegate and dosomething in the following code are actually done for you. Therefore, you only need to write
VaR T = dosomething <int> (7, 8, (x, y) => X * Y); such a row.
Public Delegate t hongjunguotest01 <t> (T T1, t T2 );
Class Program
{
Private Static t dosomething <t> (T T1, t T2, hongjunguotest01 <t> match)
{
Return match (T1, T2 );
}
Static void main (string [] ARGs)
{
VaR T = dosomething <int> (7, 8, (x, y) => X * y );
Console. writeline (t );
Console. Readline ();
}
}
The following statements are also correct (you only need to modify the code in the main function and do not need to change it elsewhere ):
VaR T = dosomething <int> (7, 8, (int x, int y) => X * y );
VaR T = dosomething <string> ("7", "8", (x, y) => X + Y );
Or we can write a more complex statement block: => a block on the right.
VaR T = dosomething <int> (7, 8, (x, y) => {If (x <5) {return (x + 8) * Y ;} else {return y ;}});
The first example is a parameter example. We will not give an example of a parameter. The following is an example without a parameter:
Public Delegate void hongjunguotest02 ();
Class Program
{
Private Static void dosomething (hongjunguotest02 match)
{
Match ();
}
Static void main (string [] ARGs)
{
Dosomething () => console. writeline ("jajaja "));
Console. Readline ();
}
}
Functional Programming
The anonymous method and lambda expressions are the products of functional programming ideas,
Functional programming is obscure from the conceptual point of view, simply put:
Function programming has no variables. All algorithms are written using recursive functions. In addition to defining functions, the entire program calls functions and has no other types of statements.