C # 3.0 Features

Source: Internet
Author: User

In our program, there are often some requirements:

1. A temporary method is needed, which is used only once or rarely.

2. The method is so short that it is shorter than the method declaration, and it is boring to write (I call it "a word method").

No way, such a way to write is really thankless, such as some button event processing, some button click is to pop up a dialog box, or call some other method. For example, the following code:

This.btnRefresh.Click + = new System.EventHandler (This.btnrefresh_click);
private void Btnrefresh_click (object sender, EventArgs e)
{
Binddata ();
}

This "Refresh" button is to do a call to Binddata () data binding method, we have to write a new method. OK, C # 2.0 provides us with an anonymous method:

This.btnRefresh.Click + = Delegate (object sender, EventArgs e) {binddata ();};

The boring code is gone. Do you want to know what's behind this?

In fact, the compiler is still behind us to do a nasty thing: it gives us a new method, it is only on the surface for us to save the code.

private void b__0(object sender,EventArgs e)
{
  this.BindData();
}

Look at the name of the method the compiler produces:

B_0,test is where this anonymous method is placed (because the time of the button I put in a test method) and one thing to note is that if this anonymous method is used in an instance method, then the behind-the-scenes method that the compiler generates for us is also an instance method, otherwise it is a static method.

Do not think the anonymous method this thing is very good, reduced a lot of code, but the use of anonymous method is not human, what is human? For example, you can read the program code in a natural language, which is a human being. In. Net The System.Collections.Generic namespace in 2.0 has some new methods in the list. For example, find, if you use anonymous methods how do we call it:

Books. Find (Delegate (BookBook) {return book. price<50;});

The code is simple, but it can't be read aloud to see how the lambda expression is written:

Books. Find (Book=>book. PRICE<50); This lambda expression can be read as follows: give you a book that returns true if its price is less than 50.

Okay, so let's go into the lambda expression:

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.