Delegated Learning (2)

Source: Internet
Author: User

1.1 delegated chain

Delegation is a multi-channel broadcast. Therefore, two or more non-delegated instances can be combined to form a delegation chain. The so-called delegated chain is the delegated method connected together in the form of a linked list.

The binary ++ and ++ = operators are used to combine the delegation in C #, and the-or-= Operator is used to remove a delegation from the delegation chain.

A new delegate is generated when you combine or remove a delegate instance from a delegate chain. The delegate has its own call list. The call list of the combined or removed delegate remains unchanged,

If the list of a delegated instance contains multiple methods, the method will be executed in the order of the call list when such a delegated instance is called. If a parameter contains a reference or output parameter (ref or out parameter ),

A reference to the same variable is used. Therefore, if you call a method in the list to modify the variable, the modified variable is used as the parameter for all delegate methods. If a delegate call contains a reference or output parameter or

A return value is determined by the last method in the call list.

Let's look at a simple example of a delegated chain!

1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 using system. threading. tasks; 6 7 namespace delegate2 8 {9 // declare delegate 10 public delegate void printhandler (string message); 11 12 // define delegate method of Class 1; 13 public class printprovider1 14 {15 public void print (string MSG) 16 {17 console. writeline ("++ 1" + MSG. tostring () + "++"); 18} 19} 20 // define the delegate method of Class 2; 21 public class printprovider2 22 {23 public void prinit (string MSG) 24 {25 console. writeline ("------ 2" + MSG. tostring () + "-------"); 26} 27} 28 29 class test 30 {31 // static delegate Method 32 static void staticprint (string MSG) 33 {34 console. writeline ("###### 3" + MSG. tostring () + "######"); 35} 36 37 static void main (string [] ARGs) 38 {39 string S = "delegate chain "; 40 41 // instance two printprovider class objects 42 printprovider1 PP1 = new printprovider1 (); 43 printprovider2 PP2 = new printprovider2 (); 44 45 // instance of delegate object 1 46 printhandler prn1 = new printhandler (pp1.print); 47 console. writeline ("\ n delegate instance 1 printed result:"); 48 prn1 (s); 49 50 // delegate object 2 instance 51 printhandler prn2 = new printhandler (pp2.prinit ); 52 console. writeline ("\ n delegate instance 2 printed result:"); 53 prn2 (s); 54 55 // delegate object 3 instance 56 printhandler prn3 = new printhandler (test. staticprint); 57 console. writeline ("\ n delegate instance 3 printed result:"); 58 prn3 (s); 59 60 // delegate chain Added 61 console. writeline ("\ n result of delegated instance PRN + prn2:"); 62 printhandler PRN = prn1 + prn2; 63 PRN (s); 64 65 console. writeline ("\ n delegate instance PRN + prn2 + prn3 Result:"); 66 PRN + = prn3; 67 PRN (s); 68 69 // delegate remove 70 console. writeline ("\ n delegated instance prn1 + prn3"); 71 PRN-= prn2; 72 PRN (s); 73 74 console. writeline ("\ n delegated instance prn3"); 75 PRN-= prn1; 76 PRN (s); 77 78 console. writeline ("\ n attempts to call an empty delegate chain to cause an exception"); 79 Try 80 {81 // delegate remove 82 // PRN = (printhandler) delegate. remove (PRN, new printhandler (pp1.print); 83 PRN-= prn3; 84 PRN (s); 85} 86 catch (nullreferenceexception ex) 87 {88 console. writeline (ex. message); 89} 90 91 console. writeline ("\ n attempts to remove the delegate from the null delegate chain, invalid"); 92 try 93 {94 PRN-= PRN; 95 PRN (s ); 96} 97 catch (nullreferenceexception ex) 98 {99 console. writeline (ex. message); 100} 101 102 console. readline (); 103} 104 105} 106}
View code


Running result:

 

I hope you can learn together and continue to study anonymous delegation tomorrow.

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.