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)