Review delegate, anonymous method, Lambda, generic delegate, Expression Tree in five minutes

Source: Internet
Author: User

Original article: http://www.cnblogs.com/xcj26/p/3536082.html

These are common things for the older generation of programmers. They are nothing new, but they are full of charm for the new generation of programmers. Many applications, such as delegation and Expression Tree, can be mastered only after long learning, understanding, and practice. Today, I try to describe it in a simple way, so that you can read this blog within five minutes.

First minute: Delegate

In some teaching materials, the blog will mention events when it comes to delegation. Although events are an example of delegation, in order to make it easier to understand, today we only talk about delegation rather than events. First, run the following code:

The following code completes a demo of the delegated application. One delegate is divided into three steps:

Public partial class WebForm3: System. Web. UI. Page {// step01: Use delegate to define a delegate. Public delegate int CalculatorAdd (int x, int y); protected void Page_Load (object sender, EventArgs e) {// step03: use this method to instantiate this delegate. CalculatorAdd cAdd = new CalculatorAdd (Add); // int result = cAdd (5, 6); int result = cAdd. invoke (5, 6);} // step02: declare a method to correspond to the delegate. Public int Add (int x, int y) {return x + y ;}}

Step 01: Define a delegate with delegate.

Step 02: declare a method to correspond to the delegate.

Step 03: use this method to instantiate the delegate.

So far, a delegate should be completed, and you can call the delegate.

Second minute: anonymous method

The last minute has already been known. It takes only three steps to complete a delegated application. If you want to make a huge strides, be careful when you get stuck. However, Microsoft is not afraid to get involved. You have to make three steps into two steps! Therefore, Microsoft uses the anonymous method to simplify the three steps above. The anonymous method is completely dispensable in C #. It is just a icing on the cake for C #. Some people give it a name called syntactic sugar.

Public partial class WebForm3: System. web. UI. page {// step01: first define a delegate public delegate int CalculatorAdd (int x, int y) with delegate; protected void Page_Load (object sender, EventArgs e) {// step02: use this method to delegate (int x, int y) {return x + y;} and assign a value to the delegate CalculatorAdd cAdd = delegate (int x, int y) {return x + y ;}; int result = cAdd. invoke (5, 6 );}}

Step 01: Define a delegate with delegate.

Step 02: use this method to delegate (int x, int y) {return x + y;} and assign a method to the delegate. In fact, this method is an anonymous method.

At this time, we will be surprised to find that this is not three steps in front of two steps. wow?

Third minute: Lambda expression

The original simple program, coupled with a few delegate keywords, the Code becomes esoteric, and fewer people who know profound things, so this can also be used as a bargaining chip for salary increases. However, Microsoft's design concept for C # is simple and easy to use. Microsoft tried to simplify the anonymous delegate (int x, int y) {return x + y;} method, and Lambda appeared. Below I will look at the writing of several lambda expressions:

Public partial class WebForm3: System. web. UI. page {public delegate int CalculatorAdd (int x, int y); protected void Page_Load (object sender, EventArgs e) {// Method 1: CalculatorAdd cAdd1 = (int x, int y) ==>{ return x + y ;}; int result1 = cAdd1 (5, 6); // Method 2: CalculatorAdd cAdd2 = (x, y) ==>{ return x + y ;}; int result2 = cAdd2 (5, 6); // method 3: CalculatorAdd cAdd3 = (x, y) => x + y; int result3 = cAdd2 (5, 6 );}}

Method 1: Remove delegate and add "=>" between () and ".

Method 2: The parameter types are eliminated Based on method 1.

Method 3: complete the operation and remove the {} and return keywords.

These methods can be written in any way, but they are difficult for beginners. I will see this method in a moment, and I will see it in a moment, which will turn people's minds upside down. If no one gives advice, I will be confused, it's hard here.

Fourth Minute: Generic Delegation

As the. net version is not upgraded, the new version will always be different from the old version. Otherwise, how can Microsoft engineers deliver to their boss? So Microsoft is playing new tricks again.

Public partial class WebForm3: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {// Method 1: Func <int, int, int> cAdd1 = (int x, int y) ==>{ return x + y ;}; int result1 = cAdd1 (5, 6); // Method 2: Func <int, int, int> cAdd2 = (x, y) ==>{ return x + y ;}; int result2 = cAdd2 (5, 6); // method 3: Func <int, int, int> cAdd3 = (x, y) => x + y; int result3 = cAdd2 (5, 6 );}}

No matter whether it is an anonymous method or a Lambda expression, the application that completes a delegate cannot skip two steps. One step is to define one delegate, and the other is to instantiate one delegate using one method. Microsoft simply merged the two steps. Use Func to simplify the definition of a delegate.

At this point, a delegated application can use Func <int, int, int> cAdd3 = (x, y) => x + y, among them, Func is called generic delegation.

Fifth minute: Expression Tree

In fact, the expression tree has nothing to do with the delegate, so we have to put it together. The expression tree is the container that stores the delegate. To be more professional, the expression tree is a data structure for accessing Lambda expressions. When Lambda expressions are used, Compile () can be obtained directly from the expressions. The following code:

public partial class WebForm3 : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        Expression<Func<int, int, int>> exp = (x, y) => x + y;        Func<int, int, int> fun = exp.Compile();        int result = fun(2, 3);    }}

Five minutes is over. My point is superficial, but at least let everyone review another delegate, anonymous method, Lambda, generic delegate, and Expression Tree.

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.