Anonymous method Instance resolution in C #

Source: Internet
Author: User
Tags new set

This article describes the anonymous methods in C # in more detail, with examples to illustrate them. We will share it with you for your reference. The specific analysis is as follows:

First, the anonymous method in C # was introduced in c#2.0, and the only way to end the c#2.0 version declaration delegate was to use the era of named methods. Although in C # 3.0 and later, LAMBDA expressions replace anonymous methods as the preferred way to write inline code. However, the information for the anonymous method also applies to lambda expressions, which can be said to be an anonymous method that has evolved over the lambda expression.

We can use anonymous methods to ignore parameter lists. This means that an anonymous method can be converted to a delegate with various signatures, which is not possible for a LAMBDA expression. Learn the anonymous method to understand the LAMBDA expression more deeply.

Before you talk about the use of anonymous methods, let's say the other names of anonymous methods. Anonymous methods are also called anonymous delegates, anonymous functions, although it is generally common, but still a little different. The MSDN official said: To pass a code block as a delegate parameter, creating an anonymous method is the only way. The anonymous method here is official, and because the anonymous method is to pass the code block as a delegate parameter, it is also called Anonymous delegate, including the author also like this name, I think it is easy to understand. As for anonymous functions, because the concept of "method" in C # is called "function" in some languages, anonymous methods are also called anonymous functions. However, in the MSDN documentation, the anonymous function includes lambda expressions and anonymous methods, it can be said that the anonymous function is a higher level, so the most official term is still anonymous method, of course, the other names are widely circulated, listening to understand on the line, there is no need to tangle.

Let's talk about the rules for how anonymous methods are written, first the example:

delegate (int i) {return i > 0;}

It follows the format of writing:

Delegate (formal parameter list) {Method body code}

Can be analogous to the anonymous function in JS to memorize.

So where does the anonymous method apply? How to use it? You can use anonymous methods when you need a temporary method that uses very little of the method or the code for the method you need is short. To give a simple example, if you need to filter out a new set of criteria in an integral type set, the following

list<int> list = new List<int> () {1, 2, 3, 4, 5, 6};//assume that all elements greater than 3 need to be fetched from the list collection to return var NewList = NewList in the new collection. FindAll (getnewlist);

Getnewlist () a method with the same signature for the other defined and delegated predicate<t> (Predicate<t> is a system built-in delegate)

Getnewlist () is defined as follows:

BOOL Getnewlist (int i) {  return i > 3;}

The above is not the way to use anonymous methods, if you use the anonymous method, you will find that everything has become so simple,

list<int> list = new List<int> () {1, 2, 3, 4, 5, 6};var newlist = list. FindAll (delegate (int i) {return i > 3;});

By contrast, you can see that using an anonymous method can provide the same functionality as a previously named method, but it no longer requires a method that was explicitly created before it was associated to a delegate, and therefore reduces the coding overhead required to instantiate the delegate, which is its greatest benefit.

It is believed that this article has certain reference value for the C # program design of everybody.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Anonymous method Instance resolution in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23560.html






Related Content C # create an SQLite console application in C # ToString data type format Daquan (thousand characters) C # implement QQ-style feature Instance code C # How to parse HTTP messages
C # Brush pen to draw a smooth pattern curve Method C # console for file read and write C # to convert milliseconds to minutes and seconds C # implementation method for configuration file encryption

Anonymous method Instance resolution in C #

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.