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.