[C # Basics] Talk about the things that entrust + = and-=

Source: Internet
Author: User

Write in front

Why do you suddenly want to talk about the Commission? The reason, starting from a colleague's idea, yesterday after work on the road has been thinking about this problem, if the Commission to register a number of methods, will be executed? For the sake of inquiry, we got a demo study.

+=

We all know that the delegate inherits from System.MulticastDelegate, and System.MulticastDelegate inherits from System.Delegate, and can register multiple methods by means of + =. So are they all executed? What is the result of the implementation? Is there a return value and no return value for the same result? Then try to talk about what the + + has done.

Test code

1 namespaceWolfy.delegatedemo2 {3      Public Delegate voidShowMsg (stringmsg);4      Public Delegate intMathoperation (intAintb);5     class Program6     {7         Staticshowmsg showmsg;8         Staticmathoperation mathoperation;9         Static voidMain (string[] args)Ten         { OneShowMsg + =Showhello; AShowMsg + =ShowHello1; -ShowMsg ("good New Year, everyone."); -Mathoperation + =Add;  theMathoperation + =Multiply; -             intresult = Mathoperation (1,2); - Console.WriteLine (Result. ToString ()); - Console.read (); +         } -         Static voidShowhello (stringmsg) +         { AConsole.WriteLine ("Hello:"+msg); at         } -         Static voidShowHello1 (stringmsg) -         { -Console.WriteLine ("Hello 1:"+msg); -         } -         Static intADD (intAintb) in         { -             returnA +b; to         } +         Static intMultiply (intAintb) -         { the             returnAb; *         } $     }Panax Notoginseng}

You can guess the results of the run, such as:

You can see that there is no return value of the output, there is a return value only output the results of the mutiply, then + = internal do what? You can look at the anti-compilation code:

1 usingSystem;2 namespaceWolfy.delegatedemo3 {4     Internal class Program5     {6         Private Staticshowmsg showmsg;7         Private Staticmathoperation mathoperation;8         Private Static voidMain (string[] args)9         {TenProgram.showmsg = (showmsg) delegate.combine (program.showmsg,Newshowmsg (Program.showhello)); OneProgram.showmsg = (showmsg) delegate.combine (program.showmsg,Newshowmsg (PROGRAM.SHOWHELLO1)); AProgram.showmsg ("good New Year, everyone."); -Program.mathoperation = (mathoperation) delegate.combine (Program.mathoperation,Newmathoperation (Program.add)); -Program.mathoperation = (mathoperation) delegate.combine (Program.mathoperation,Newmathoperation (program.multiply)); theConsole.WriteLine (Program.mathoperation (1,2). ToString ()); - Console.read (); -         } -         Private Static voidShowhello (stringmsg) +         { -Console.WriteLine ("Hello:"+msg); +         } A         Private Static voidShowHello1 (stringmsg) at         { -Console.WriteLine ("Hello 1:"+msg); -         } -         Private Static intADD (intAintb) -         { -             returnA +b; in         } -         Private Static intMultiply (intAintb) to         { +             returnAb; -         } the     } *}

The above code can be seen in the + = inside the delegate by the Combine static method to combine the delegate, you can see how the delegation of this static method is implemented.

You can see the final call to Combineimpl this method, this method inside is very strange:

There is no code that we want to see, so what does this method do?

Explanation of MSDN

Concatenates the invocation lists of the specified multicast (combinable) delegate and the current multicast (combinable) Delegate.

The idea is to add the current delegate to the specified set of multicast delegates.

Around a circle so there is a return value of the delegate, in the end executed it? That can only be seen through debugging. (around a circle, back to the editor, alas)

Continue F11 you'll find that you did enter the Add method

It does, but it overwrites the previous value when traversing the multicast delegate collection.

So now you can conclude that there is no return value of the delegate, how many methods you register to it, how many methods it executes, and the delegate that returns the value, how many methods are executed by the same number of methods, but returns the return value of the last method.

-=

Since said + =, so as to clean up the mess of-= also have to say. The use of + = In the project is used to release with-=. So what did it do inside? Also use the above code, after outputting the result, use-= to release the resource.

As you can see, using-= internally is the Remove static method that invokes the delegate.

Using-= ultimately is to set the delegate to null, and the other means null reference, so you can wait for the garbage collector to recycle.

Summarize

Although the problem is very basic, a colleague asked at that time, he said to him, on the way from work has been thinking, how the interior is achieved? Try to find out by way of anti-compilation. But seemingly combineimpl this method, the results are not very satisfied. Did not see the specific implementation. Hope to help you!

[C # Basics] Talk about the things that entrust + = and-=

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.