09.c# delegate Conversions and anonymous methods (Chapter five 5.1-5.4)

Source: Internet
Author: User

Today will read the book, I want to write out for your reference, the shortcomings please correct me. Go to the chase.

Developing Web Forms in c#1 often encounters usage events, creating an event-handling method for each event, using new EventHandler () in assigning methods to events, and different events having instances of different EventHandler derived classes. Because I use the console App here, the principle is the same, and look at

//define a delegateDelegate voidPrinter ();Static voidMain (string[] args) {Printer P=NewPrinter (PRINT1); P+=NewPrinter (Print2); P+=NewPrinter (PRINT3);    P.invoke (); Console.readkey ();}Static voidPrint1 () {Console.WriteLine ("Print1");}Static voidPrint2 () {Console.WriteLine ("Print2");}Static voidPrint3 () {Console.WriteLine ("Print3");}

You can see that each time you delegate a method to P, you want new Printer (), the parameter is the method to pass in, you can imagine that in different delegates, it is too tedious to explicitly create various delegates with code. In C#2, the implicit conversion from a method group to a compatible delegate type is assumed to be understood as a copy constructor in C + +, such as Printer p = Print1, which actually calls the 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 constructor of the Printer class, and the + = operation should be a Printer type overloaded + operation for two Printer classes to add, while the method group implements the implicit conversion, Print2 and Print3 are implicitly converted to instances of the printer class, c#2 such operations reduce the input of the code and are more intuitive, such as " I just give a way to an object, let it do it for me, I don't care what kind of type you want to send."

    1. Covariance of generic delegates
    2. Inverse of the delegate return

For the 1th, it can be understood that to delegate a method, the delegate created by this method can be a derived class of another delegate, such as the base class for all event handling is EventHandler, when an event needs to pass in a method, You can use this method to create an instance of a class derived from EventHandler, without the event I will not use the code to express, please Daniel told .

For 2nd, the delegate defines the return type, which can be used to create a delegate if the return type has a derived class, such as a derived class that is returned in the implemented method.

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------

Anonymous methods, anonymous methods use the delegate keyword, which allows you to specify an inline delegate instance operation, and the anonymous method returns an instance of the delegate.

Many of the built-in generic delegates in c#2, such as Action (T),predicate<t> and so on, can use anonymous functions when instantiating delegates. Such as

1action<int> A =Delegate(intx) {Console.WriteLine (x);};2predicate<BOOL> b =Delegate(BOOLx) {returnx = =true; };3A.invoke (1);4B.invoke (1==1);5A2);6b2==3);

Note that the anonymous method returns a delegate instance. If you just want to deal with something without parameters, consider omitting the argument. This reminds me of the point that when a program goes wrong, if you do not log parameters, you can print "error" directly, and for anonymous functions, you can omit the parameters.

1 New System.Threading.Thread (delegate (object  x) {}); 2 New System.Threading.Thread (delegate() {});

In fact, the above is wrong , in fact, very annoying some words, write a lot of things, they look like a little truth, and then suddenly tell you "those things wrong point of view", this time I also come to use.

As I said before, the anonymous function returns an instance of a delegate, and I'm really a method of factoring in an anonymous method because there is an implicit conversion (that is, a copy constructor in C + +, or a single-argument constructor) that returns a match to the new System.Threading.Thread () A suitable parameter type, so I think the parameter is not omitted, the book is can be omitted, in fact, just System.Threading.Thread have two overloaded version, their own ideas , the wrong please Daniel correct, the younger brother greatly appreciated.

Please treatise.

09.c# delegate Conversions and anonymous methods (Chapter five 5.1-5.4)

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.