Do you know the evolution of Lambda expressions in C ?,

Source: Internet
Author: User

[Switch] Do you know the evolution of Lambda expressions in C ?,

[Switch] Do you know the evolution of Lambda expressions in C?

It has to start from a long time ago. Remember that time...

I can't help but remember that there was something called delegation that was so difficult to understand.

Use of delegation

Example 1:

What is delegation?

Personal Understanding:Type used to pass the Method. (The numeric types include int, float, and double.)

Example 2:

After a long time...

Anonymous Method

In many cases, the method for receiving a delegate is one-time or the method body is very simple...

Example 3:

We can write it as follows:

Have you found that we need to define the delegate every time? In many cases, the signature may be the same. In this way, there is no need to define duplicates.

Then it took a long time...

Func and Action

Maybe the boss also thinks that every time we define the delegate is a bit silly, so we simply define all the delegates that may be used at once in the framework. How can the ever-changing method signature be fully defined? It doesn't matter. Can it be defined as generic.

Func:

Careful friends may see that Func defines an int as opposed to AddDelegate. The extra one refers to the return type. Let's look at the corresponding definition in F12:

The above Func statement can be abbreviated :(The syntax is just sugar. After compilation, the code is still annotated.)

Let's look at the Action:

Reminder: if we write code to delegate... in the future, do you need to immediately think about whether it can be replaced by Func or Action? In C #4, the Action and Func have 16 parameters, which is enough for you to use.

We waited and waited, and it took a long time...

The birth of Lambda

XX, this TM is a brother. Directly remove the delegate keyword and add a => to the lambda expression. (=> Read as goes)

We continue to simplify:

It is also possible to discard the parameter type, because powerful VS can deduce the parameter type based on the generic delegate Func.

Can it be simplified? Of course:

The return keyword is no longer needed, and the braces are no longer needed. (Of course, only one statement can be used in the method body.)

Now it looks very close to the Lambda expression we usually use.

If there is only one input parameter, we can continue to simplify it:

This is what we see most in lambda.

There are also requirements to grow like this:

About 1st, lambda usually uses IEnumerable or IQueryable, so only one parameter can be satisfied.

For the 2nd point, we use the extension method for chained programming.

For example:(Use chain to solve the second point)

Since then, we have lived a happy life...

 

The following figure shows how to understand C # in depth:

 

TIPS:(Asynchronous Lambda)

Func <Student, Task <bool> func = async t => {await Task. Delay (100); // wait for 100 milliseconds and return false ;};

 

End:

This article briefly describes the step-by-step evolution of lambda expressions in C. Not necessarily true. Pat it!

This article synchronizes to C # basic knowledge consolidation Series

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.