C # One of the important features of delegation,
The composition of the delegate must meet the following four conditions:
The delegated packaging method must meet the following conditions:
- The method signature must be consistent with the delegate signature. The method signature includes the number, type, and order of parameters;
- The return type of the method must be the same as that of the Delegate. Note that the return type of the method is not part of the method signature.
Example 1:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace ConsoleAppDelegate {class Program {// 1. Use the delegate keyword to define a delegate type delegate void MyDelegate (int parm1, int parm2); static void Main (string [] args) {// 2. Declare the delegate variable d MyDelegate d; // 3. instantiate the delegate type. The passed method can also be a static method, the instance method d = new MyDelegate (new Program () is passed here (). add); // 4. The delegate type is passed to another method MyMethod (d); Console. read ();} // The definition of this method must be the same as that of the delegate, that is, the return type void, and the void Add (int parm1, int parm2) parameters of the two int types) {int sum = parm1 + parm2; Console. writeLine ("sum of two numbers:" + sum);} // the parameter of the method is the delegate type private static void MyMethod (MyDelegate mydelegate) {// 5. Call the delegate in the method // mydelegate. invoke (1, 2); mydelegate. invoke (1, 2 );}}}
Example 2:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace ConsoleAppDelegateGreeting {class Program {static void Main (string [] args) {Program p = new Program (); p. greeting ("cangjing Kong", p. chineseGreeting); p. greeting ("Tommy Li", p. englishGreeting); Console. read () ;}// define the delegate Type public delegate void GreetingDelegate (string name); // You can implement the public void Greeting (string name, greetingDelegate callback) {// call the delegate callback (name);} // American greeting method public void EnglishGreeting (string name) {Console. writeLine ("Hello," + name);} // Chinese greeting method public void ChineseGreeting (string name) {Console. writeLine ("hello," + name );}}}
Summary:
- The delegate encapsulates behaviors that contain special return types and a set of parameters, similar to interfaces that contain a single method;
- The type signature described in the delegate type declaration determines which method can be used to create a delegate instance and the call signature;
- In order to create a delegated instance, a method and (for instance methods) Call method are required;
- Delegated instances are not easy to change;
- Each delegated instance contains a call list-an operation list;
- The delegated instance can be merged or deleted from one delegated instance;
- The event is not a delegated instance ----- It is just a pair of add/remove methods (similar to the attribute value method/value assignment method)