Multicast delegation (from www. sysoft. CC)

Source: Internet
Author: User

A real column of the delegate type can be bound to not only one method, but also multiple methods, that is, multicast delegation or delegation chain.
As follows:

Namespace multicast delegate {// defines the delegate type variable Public Delegate int weito () with reverse return value and no parameters; Class program {static void main (string [] ARGs) {// create a real column of the corresponding delegate type, specify the method, and add the method to implement the delegate multicast. // The delegated execution is generally performed in the order of adding methods. Weito KK = new weito (M1); KK + = m2; KK + = m3; KK + = M4; console. writeline (kk (); // multicast delegate with reverse return value, only the return value of the last added method can be returned. Console. writeline ("------------------ obtain the reverse return value of the multicast delegate method through traversal ---------------"); Delegate [] delkks = KK. getinvocationlist (); // you can use traversal to call each method in the delegate to obtain the return value in the delegate step by step. // You can use. getinvocationlist () to obtain all the methods stored in the current delegate. // Its reverse return value type is a delegate [] array (delegate array) // all its reverse delegation inherits from the image class multicastdelegate, while the multicastdelegage inherits from the image class delegate. // The multicast delegate is to forcibly convert multiple methods bound to the delegate into a delegate object and store them in an array of objects named _ invocationlist. // When the delegate is called, traverse the object array and execute the corresponding function. Foreach (delegate itemdelegate in delkks) {weito method = (weito) itemdelegate; int K = method (); console. writeline (k);} console. readkey ();} static int M1 () {return 1;} static int m2 () {return 2;} static int m3 () {return 3;} static int M4 () {return 4 ;}}

}

Note: 1. When an exception occurs in the method bound to the multicast delegate, the method after this method will not be executed.
2. You can add or remove a delegate to bind a method only with '+ =' or '-='. If you use '=' directly, all methods bound to the delegate variable will be overwritten.
3. The first method bound to the multicast delegate can only be '+ = '.
4. The delegate is the same as the string and has the immutable feature.
Principle of Multi-play delegated memory allocation (delegate K = new delegate (M1); k + = k2 ;)

Multicast delegation (from www. sysoft. CC)

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.