[. NET] C # knowledge Review,
C # Review of knowledge-delegate (continued)
[Blogger] Anti-bone Aberdeen [original] http://www.cnblogs.com/liqingwen/p/6046171.html
Collation
In the previous article "C # knowledge Review-delegate", we have introduced the basic knowledge of delegation. Here we will provide additional instructions and deepen our understanding.
Directory
- Two simple demos: Delegate with naming method and delegate with anonymous method
- Create multicast delegate
- Simple evolution of delegation
I. Two simple demos: The delegate with the naming method and the delegate with the anonymous method
The delegate can be associated with the naming method. When a delegate is instantiated using a naming method, this method is passed as a parameter, for example:
1 class Program 2 {3 // declare a delegate 4 delegate void MyDel (string message); 5 6 7 static void Main (string [] args) 8 {9 // use the static method as the parameter instantiation delegate 10 MyDel del = Print; 11} 12 13 // declare a method 14 private static void Print (string message) 15 {16 Console. writeLine (message); 17} 18}
This is called the naming method. The delegate constructed using the naming method can encapsulate static methods or instance methods. In earlier versions of C #, naming is the only way to instantiate a delegate. However, if you do not want to pay the system overhead for creating a new method, C # enables you to instantiate the delegate and immediately specify the code block to be processed when the delegate is called. A code block can contain lambda expressions or anonymous methods.
[Note] ① The method passed as the delegate parameter must have the same signature as the delegate declaration. ② The delegated instance can encapsulate static or instance methods. ③ Although the delegate can use the out parameter, we recommend that you do not use it for multi-channel broadcast event delegation because you cannot know which delegate will be called. Example 1: The following is a simple example of declaring and using delegation. Note that the delegate MyDel and the associated method Print have the same signature (even if the parameter names m and n of the method are replaced ).
1 class Program 2 {3 // declare a delegate 4 delegate void MyDel (int n, int m); 5 6 static void Main (string [] args) 7 {8 // use the static method Print as the parameter instantiation delegate 9 MyDel del = Print; 10 Console. writeLine ("You are ready. You have to call the delegate! "); 11 12 for (int I = 0; I <10; I ++) 13 {14 del (I, 1); 15} 16 17 Console. read (); 18} 19 20 // declare a Method 21 private static void Print (int m, int n) 22 {23 Console. write (m-n + ""); 24} 25}
1 class Program 2 {3 // declare a delegate 4 delegate void MyDel (int n, int m); 5 6 static void Main (string [] args) 7 {8 // use the static method Print as the parameter instantiation delegate 9 // MyDel del = Print; 10 11 // use the anonymous method 12 MyDel del = delegate (int m, int n) 13 {14 Console. write (m-n + ""); 15}; 16 Console. writeLine ("You are ready. You have to call the delegate! "); 17 18 for (int I = 0; I <10; I ++) 19 {20 del (I, 1); 21} 22 23 Console. read (); 24} 25}
[Note] thanks to the 520 amendment attached to the second floor. I would like to express my appreciation for the addition of the alilang on the 4th floor. You can also use Lambda (m, n) => to create a delegate.
Ii. Create multicast Delegation
This example shows how to create a multicast delegate. A useful property of the delegate object is:
Available+The operator assigns multiple objects to a delegated instance. Multicast delegates include the list of assigned delegates. When a multicast delegate is called, it calls the delegate in the list in sequence. Only delegates of the same type can be merged.-The operator can be used to remove a component delegate from a multicast delegate.
1 class Program 2 {3 // declare a delegate 4 delegate void MyDel (); 5 6 static void Main (string [] args) 7 {8 // Action: you can also try using Action instead of MyDel to try 9 10 MyDel del = Start; // create a delegate object 11 MyDel del2 = Stop; // create a delegate object 12 MyDel del3 = del + Stop; // merge the first two delegate objects 13 MyDel del4 = del3-Start; // remove a delegate object 14 15 Console. writeLine ($ "This is {nameof (del)}:"); 16 del (); 17 Console. writeLine ($ "This is {nameof (del2)}:"); 18 del2 (); 19 Console. writeLine ($ "This is {nameof (del3)}:"); 20 del3 (); 21 Console. writeLine ($ "This is {nameof (del4)}:"); 22 del4 (); 23 24 Console. read (); 25} 26 27 private static void Start () 28 {29 Console. writeLine ($ "{nameof (Start )}... "); 30} 31 32 private static void Stop () 33 {34 Console. writeLine ($ "{nameof (Stop )}! "); 35} 36}
Evolution process.
1 class Program 2 {3 // declare a delegate 4 delegate void MyDel (); 5 6 static void Main (string [] args) 7 {8 // The following are statements for different versions and the method for initializing delegation 9 10 // ≥c #111 MyDel del1 = new MyDel (Print ); 12 13 // ≥c #214 MyDel del2 = Print; // previous simplified version 15 MyDel del3 = delegate () 16 {17 Console. writeLine ($ "{nameof (Print )}... "); 18}; // anonymous method 19 20 // ≥c #321 MyDel del4 = () => 22 {23 Console. writeLine ($ "{nameof (Print )}... "); 24}; // Lambda expression 25 26 Console. read (); 27} 28 29 private static void Print () 30 {31 Console. writeLine ($ "{nameof (Print )}... "); 32} 33}
Portal
C # knowledge Review-serialization
C # knowledge Review-Expression Tree Expression Trees
C # knowledge Review-feature Attribute and AssemblyInfo. cs-understanding common feature attributes [Reference] Microsoft official documentation