C # Lambda expression understanding: Using Lambda in the anonymous method of the predicate Method

Source: Internet
Author: User
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 ();

}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.