C #3.0 features-Lambda expressions

Source: Internet
Author: User

Lambda expressions are introduced by MS in C #3.0. Lambda expressions were first seen in the LISP language, and American mathematician Alonzo Church defined them in 1936. This expression provides a simple way to describe an algorithm.
Before introducing Lambda expressions, let's take a look at how an algorithm is passed as a parameter to a method.
Use Named Methods)

Before C #2.0, you can use a delegate to complete this task. For example, an application that filters array elements according to certain rules can be written as follows for common modules:

Public class Common
{
Public delegate bool IntFilter (int I );

Public static int [] FilterArrayOfInt (int [] ints, IntFilter filter)
{
ArrayList aList = new ArrayList ();
Foreach (int I in ints)
{
If (filter (I ))
{
AList. Add (I );
}
}

Return (int []) aList. ToArray (typeof (int )));
}
}

Then the algorithm developer writes the filtering algorithm into a function, such as the following:

Public class Application
{
Public static bool IsOdd (int I)
{
Return (I & 1) = 1 );
}
}

The IsOdd () method is the named method that describes the algorithm. In the actual call process, caller calls the FilterArrayOfInt () method of the Common class. The second parameter of this method is the delegate of a filter algorithm (function pointer ). All filter algorithms must have the same parameters and return value types. When calling the FilterArrayOfInt () method, take the IsOdd () that describes the algorithm as the parameter and substitute it into the delegate, so that an event can be done.

Static void Main (string [] args)
{
Int [] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Int [] oddNums = Common. FilterArrayOfInt (nums, Application. IsOdd );

Foreach (int I in oddNums)
{
Console. WriteLine (I );
}
}

The results are of course 1, 3, 5, 7, and 9. The implementer of an algorithm can write different filters.
Use the Anonymous method (Anonymous Methods)

In C #2.0, the anonymous method is introduced to replace the delegate by Inline code. In the above example, the oddNums value assignment in the Main () method can be rewritten as follows:
Int [] oddNums = Common. FilterArrayOfInt (nums, delegate (int I) {return (I & 1) = 1 );});
This inline form can be simply used in the place of the delegate parameter. Since no Method for Determining the name is defined, it is called an anonymous method. For code that does not need to be reused, this method can greatly simplify the program structure, but reduce readability. It is suck for complicated function bodies ..
Use Lambda expressions

A Lambda expression is a list of parameters separated by commas (,), followed by a lambda operator, followed by an expression or statement block. Multiple Input parameters must be enclosed by parentheses. In C #, the lambda operator is =>. Therefore, the lambda expression in C # should look like this:

(Param1, param2 ,... ParamN) =>
{
Statement1;
Statement2;
...
StatementN;
Return (lambda_expression_return_type );
}

 

Let's look at the delegate part. A delegate actually specifies the format of the input parameter and the format of the returned value. In lambda expressions, it corresponds to the lump on the left and the return stuff in the statement body. In the previous example, the input is an int type data and returns a bool volume, for example:
X => x. Length> 0
This expression can be read as "x goes to x. Length> 0", or "input x, returns x. Length> 0 ". The following lambda expression returns the length of the input parameter:
S => s. Length
Then, delegate should specify the return int value. For multiple input variables, such:
(X, y) => x = y
A little more complicated:

(X, y) =>
{
If (x> y)
Return (x );
Else
Return (y );
}

Well, let's transform the previous example. The lambda expression designed by the algorithm designer must satisfy the delegate statement:
Delegate bool IntFilter (int I); www.2cto.com
You can write the value assignment of oddNums as follows:
Int [] oddNums = Common. FilterArrayOfInt (nums, I => (I & 1) = 1 ));
Of course, the results will be exactly the same as before.


Author: GUMINCONG

Related Article

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.