From anonymous method to Lambda expression deduction process, lambda deduction

Source: Internet
Author: User

From anonymous method to Lambda expression deduction process, lambda deduction

Lambda expressions are anonymous functions that can be used to create a delegate or expression directory tree. By using lambda expressions, you can write local functions that can be passed as parameters or returned as function call values. The above is the description of the Lambda expression on the msdn official website. Essentially, Lambda expressions are derived from anonymous methods. To improve the productivity of our programmers, Microsoft programmers have launched Lambda expressions, which are undoubtedly very influential.

Lambda expressions use the Lambda operator =>. We generally read it as "goes to". Of course, this does not mean that it has the same meaning as the goto in C, it means "transfer. The writing rules of Lambda expressions are as follows:

Parameter List => Expression

Now let me explain the deduction process from the anonymous method to the Lambda expression. For example,

// Assume that you need to obtain all the elements greater than 3 from the list set and return the List <int> list = new List <int> () {1, 2, 3, 4, 5, 6}; var newlist = list. findAll (delegate (int I) {return I> 3 ;});

This is implemented using the anonymous method. Microsoft programmers think that this is not simple enough. In order to simplify the writing, they have the following inference process.

First, since you always need to input an anonymous method, there will always be the keyword delegate, so you can consider removing delegate. If delegate is removed, the writing is not standardized, so the => operator is introduced to connect the parameter list and expression, and the preceding formula becomes:

var newlist = list.FindAll((int i) => { return i > 3; });

Second, the int type can be inferred. You can see it when you point to "FindAll" with the mouse:

,

This lets us know that the subsequent input parameters of delegate are int type (in this example, It is int type, because the above set is int type, in fact, what type depends on your needs ), therefore, it is unnecessary to specify the int type. Therefore, you can write it as follows:

var newlist = list.FindAll((i) => { return i > 3; });

At this time, there is only one parameter, so the brackets can also be removed:

var newlist = list.FindAll(i => { return i > 3; });

Then, FindAll () needs to input a Predicate <T> generic delegate. We can see from F12 that the return value type of this delegate is bool, similarly, the passed anonymous method must have the same signature as it, so the bool value must also be returned. Because you always know the type of value to be returned, you can consider removing the return keyword, and there is only one expression in the method body, so you can consider removing the braces, at the same time, remove unnecessary semicolons after the expression. At this time, the original method body has become an I> 3 expression, which is equivalent to a bool value, similar to if (I> 3), multiple return keywords are used only to return values to conform to the method-defined specifications. So the original formula evolved:

var newlist = list.FindAll(i => i > 3 );

This is the Lambda expression.

After reading the above example, some people may have such doubts: => only one expression can be written on the right. What if I want to execute complicated logic? In fact, you can write as follows:

List <int> newlist = list. findAll (int I) => // parentheses and int can be omitted at the same time, or only int can be omitted, not just parentheses left int {// here you can write complex logic, the following is just an example. Of course, you can directly return I> 3; the result is the same as the above Console. writeLine ("the original set has the following numbers: {0}", I); bool B = I> 3; return B ;});

In the previous example, only one parameter on the Left can omit parentheses. In fact, when there are more than one parameter, parentheses cannot be omitted. For example:(I, j) => expression or statement Block

If there is no parameter, you can write it like this:() => Expression or statement Block

From an anonymous method to a Lambda expression, we can say that a Lambda expression is a delegate type, which can be passed as a delegate parameter like an anonymous method.


C # What are the anonymous types and Lambda expressions?

To be honest, I don't understand either.

It seems to be 3.0 new things. I have referred to the following materials.

As for lambda expressions...

I found it on MSDN ..
Msdn.microsoft.com/zh-cn/library/bb213687.aspx
Previously, I only saw it in python ~~

Soft.ccw.com.cn/...shtml
Reference: soft.ccw.com.cn/....shtml

Lambda expressions are not understood. Here is a simple example,

The biggest function is to use anonymous functions and linq queries.
This is used on the anonymous method:
Delegate int del (int I );
Del myDelegate = x => x * x;
Int j = myDelegate (5); // j = 25
Equivalent
Delegate int del (int I );
Del myDelegate = delegate (int I) {I = I * I ;};
Int j = myDelegate (5); // j = 25
As for the future of linq, do not go into details.

Direct I + 1 ???
Haha, you have not encountered some situations that must be delegated.
For example, for cross-thread calls, you can only use delegates, while lambda expressions are a very convenient way of writing. This is purely for convenience.

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.