Lambda expressions
"Lambda expression" is an anonymous function and an efficient function-like programming expression. Lambda simplifies the amount of code to be written during development. It can contain expressions and statements and can be used to create a delegate or expression directory tree type. It supports inline expressions with input parameters that can be bound to the delegate or Expression Tree. All lambda expressions use the lambda operator =>, which is read as "goes ". The left side of the lambda operator is the input parameter (if any), and the right side is the expression or statement block.
The following three methods will help you easily understand the benefits of lambda expressions, which I have seen in other articles. Now I will share with you the code below:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Threading. tasks;
Namespace lamdbademo
{
Class Program
{
Private Static void findlistdelegate ()
{
// Create a generic list class first
List <string> List = new list <string> ();
List. addrange (New String [] {"Java course", "J2EE course", "ado.net course", "Data Structure Course "});
Predicate <string> findpredicate = new predicate <string> (isbookcategory );
List <string> bookcategory = List. findall (findpredicate );
Foreach (string STR in bookcategory)
{
Console. writeline ("{0} \ t", STR );
}
}
// The predicate method. This method is passed to the findall Method for book classification judgment.
Private Static bool isbookcategory (string Str)
{
Return Str. endswith ("Course ")? True: false;
}
// Use the anonymous method for the search process
Private Static void findlistanonymousmethod ()
{
// Create a generic list class first
List <string> List = new list <string> ();
List. addrange (New String [] {"Java course", "J2EE course", "ado.net course", "Data Structure "});
// Here, create a code block for the delegate directly using the anonymous method, instead of creating a separate Method
List <string> bookcategory = List. findall (delegate (string Str)
{
Return Str. endswith ("Course ")? True: false;
}
);
Foreach (string STR in bookcategory)
{
Console. writeline ("{0} \ t", STR );
}
}
// Use Lambda to implement the search process
Private Static void findlistlambdaexpression ()
{
// Create a generic list class first
List <string> List = new list <string> ();
List. addrange (New String [] {"Java course", "J2EE course", "ado.net course", "Data Structure "});
// Here, use Lambda to create a delegate Method
List <string> bookcategory = List. findall (string Str) => Str. endswith ("Course "));
Foreach (string STR in bookcategory)
{
Console. writeline ("{0} \ t", STR );
}
}
Static void main (string [] ARGs)
{
Console. writeline ("Traditional delegated code example :");
Findlistdelegate ();
Console. Write ("\ n ");
Console. writeline ("example of using anonymous methods :");
Findlistanonymousmethod ();
Console. Write ("\ n ");
Console. writeline ("example of using lambda :");
Findlistlambdaexpression ();
}
}
}