Lambda expression diagram, lambda expression
Internal delegate int MyDel (int x); public class Lambda {private MyDel del = delegate (int x) {return x + 1 ;}; // anonymous method private MyDel del2 = (int x) =>{ return x + 1 ;}; // Lambda expression private MyDel del3 = (x) ==>{ return x + 1 ;}; // Lambda expression private MyDel del4 = x => x + 1; // Lambda expression}
Where does the lambda expression come from?
Lambda Expressions (Lambda Expressions) 16: 33 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:
Www.ondotnet.com/..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) ...... remaining full text>
Who will explain the lambda expression in detail?
Simply put, it is an anonymous delegate.
See msdn
Msdn.microsoft.com/zh-cn/library/bb213687.aspx
Reference: msdn.microsoft.com/zh-cn/library/bb213687.aspx