Simple use case of lmbda expressions

Source: Internet
Author: User

Lambda expressions are anonymous functions that can contain expressions and statements and can be used to create a delegate or expression directory tree type. All lambda expressions use the lambda operator =>, which reads "goes ". The left side of the lambda operator is the input parameter (if any), and the right side contains the expression or statement block. Lambda expressions x => X * X are read as "X goes to X times X ". (From Baidu encyclopedia)

The following are some examples of your own use.

1. WCF call (use of Lambda on the event)
Example:
// Traditional Method
Public void testwcf ()
{
Securitybusinessserviceclient client = new securitybusinessserviceclient ();
Client. updatedepartmentcompleted + = new eventhandler (client_updatedepartmentcompleted );
// Other code
}

Private void client_updatedepartmentcompleted (Object sender, eventargs E)
{
// Other code B
}

// Lambda expression
Public void testwcf ()

{
Securitybusinessserviceclient client = new securitybusinessserviceclient ();
Client. updatedepartmentcompleted + = new eventhandler (
(Sender, e) =>
{
// Other code B
});
// Other code
}

2. Parallel loop (must be used for Parallel Loops)
Example:
Public void looptest (list source)
{
Parallel. foreach (source, item =>
{
System. Threading. thread. Sleep (100 );
});
}
Note that a Lambda expression cannot contain goto statements, break statements, or continue statements whose target is located outside or inside the body of the anonymous function.

3. List function (use of Lambda on the set function)
Example:
List numlist = new list ();
// Implement find
Int x = numlist. Find (n => N = 3 );

// Implement sort
Numlist. Sort (x, y) =>{ return X. compareto (y );});

// Implement foreach
// Note that a Lambda expression cannot contain goto statements, break statements, or continue statements whose target is located outside or inside the body of the anonymous function.
Numlist. foreach (n => otherlist. Add (n ));

// Implement where
Numlist. Where (n => n. A = 0 );

// Implement orderby
Numlist. orderby (n => n. );

// Similar to groupby, todictionary, toarray
Lambda can be used not only on the list, but also on other sets, such as arrayliat, dictionary, and able.

4. List Lambda can replace the simple LINQ
Example:
// In the LINQ format
Ienumerable excessivecoveragelist = NULL;
Excessivecoveragelist = from item in outputinfo
Where item. cityname = "Xi'an"
Select item;
List list = excessivecoveragelist. tolist ();

// Lambda
List list = outputinfo. Where (item => item. cityname = "Xi'an"). tolist ();

I found relevant information on the Internet, saying that the lamdba expression is only improved at the syntax level, not.. NET provides new commands, taking Lambda as an example to replace simple LINQ. net, the effect is basically equivalent.

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.