09. C # Delegate conversion and anonymous methods (chapter 5, 5.1-5.4 ),

Source: Internet
Author: User

09. C # Delegate conversion and anonymous methods (chapter 5, 5.1-5.4 ),

Today, I will write what I want to read in the book for your reference. please correct me if you have any shortcomings. Go to the topic.

When developing a web form in C #1, you often encounter use events. Create an event handling method for each event. When assigning a method to an event, new EventHandler () is used (), different events have different EventHandler derived class instances, because when I use the Console App here, the principle is the same.

// Define a delegate void Printer (); static void Main (string [] args) {Printer p = new Printer (Print1); p + = new Printer (Print2 ); p + = new Printer (Print3); p. invoke (); Console. readKey ();} static void Print1 () {Console. writeLine ("print1");} static void Print2 () {Console. writeLine ("print2");} static void Print3 () {Console. writeLine ("print3 ");}

We can see that new Printer () is required every time p is entrusted with a method. The parameter is the method to be passed in. It can be imagined that when different delegates, using code to explicitly create various delegates is too cumbersome. In C #2, implicit conversions from a method group to a compatible delegate type are supported. You can take it for granted as a copy constructor in C ++, for example, Printer p = Print1 actually calls new Printer (Print1). The Code is as follows.

1 Printer p = Print1;2 p += Print2;3 p += Print3;4 p.Invoke();

Printer p = Print1 calls the Printer class constructor, while the + = operation should be a Printer type overload + operation for adding two Printer classes, and the method group implements implicit conversion, print2 and Print3 are implicitly converted to Printer class instances. operations such as C #2 reduce the input of Code and are more intuitive. For example, "I just gave a method to an object, let it help me execute it. I don't care what type you want to upload ".

The first point is to delegate a method. The delegate created using this method can be another delegate's derived class. For example, the base class for processing all events is EventHandler, when an event needs to pass in a method, you can use this method to create an instance of classes derived from EventHandler. I will not use the code to represent the events without using the event. Please let me know.

For the second point, in the delegate definition return type, if the return type has a derived class, if its derived class is returned in the implemented method, you can use this method to create the delegate.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Anonymous method: The delegate keyword is used for anonymous methods. You can specify an inline delegate instance operation. An anonymous method returns a delegate instance.

Many built-in generic delegates in C #2, such as Action (T) and Predicate <T>, can use anonymous functions during delegation instantiation. For example

1 Action<int> a = delegate (int x) { Console.WriteLine(x); };2 Predicate<bool> b = delegate (bool x) { return x == true; };3 a.Invoke(1);4 b.Invoke(1 == 1);5 a(2);6 b(2 == 3);

Note that the anonymous method returns a delegated instance. If you only want to handle a few things without parameters, You can omit the parameters. This reminds me that when a program error occurs, if the parameter is not recorded, "error" can be printed directly. For anonymous functions, the parameter can be omitted.

1 System.Threading.Thread th = new System.Threading.Thread(delegate (object x) { });2 System.Threading.Thread th2 = new System.Threading.Thread(delegate() { });

In fact, the above statements are wrong. In fact, I hate some things and write a lot of things. I think it seems a bit reasonable, and then I suddenly tell you "wrong ideas about things ", I will also use it this time.

As mentioned above, the anonymous function returns a delegate instance. I solve it as an anonymous method, because there is an implicit conversion (that is, the copy constructor in C ++, or single-parameter constructor), it returns a new System. threading. A proper parameter type in Thread (), so I think the parameter cannot be omitted. In the book, it can be omitted, but it is actually just a System. threading. thread has two overloaded versions. I think it's my own idea. please correct me if you are wrong. Thank you very much.

Please make an axe.

 

Reference page: http://qingqingquege.cnblogs.com/p/5933752.html

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.