The composition of a delegate must meet the 4 conditions:
- declaring the delegate type;
- There must be a method that contains the code to be executed;
- A delegate instance must be created;
- The delegate instance must be called (Invoke)
The method of entrusting packing needs to meet the following conditions
- The signature of the method must be consistent with the delegate, and the method signature includes the number, type and order of the parameters;
- The return type of the method is to be consistent with the delegate, note that the return type of the method is not part of the method signature
Example one:
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, declaring a delegate variable d mydelegate D; 3, the instantiation of the delegate type, the passed method can also be a static method, here is passed the instance method D = new MyDelegate (new program (). ADD); 4. The delegate type is passed as a parameter to another method MyMethod (d); Console.read (); } The definition of the method must be the same as the delegate definition, that is, return type void, two arguments of type int, void Add (int parm1,int parm2) { int sum = Parm1 + parm2; Console.WriteLine ("The and of the two numbers for:" +sum); } The parameter of the method is the delegate type private static void MyMethod (MyDelegate mydelegate) { //5, which is called in the method delegate //mydelegate. Invoke (1, 2); MyDelegate. Invoke (1, 2);}}
Example two:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleappdelegategreeting{classProgram {Static voidMain (string[] args) {Program P=NewProgram (); P.greeting ("Cang Jing Empty", p.chinesegreeting); P.greeting ("Tommy Li", p.englishgreeting); Console.read (); } //Defining delegate Types Public Delegate voidGreetingdelegate (stringname); //after having a delegate, you can implement the Greeting method as follows Public voidGreeting (stringName,greetingdelegate Callback) { //Invoke Delegatecallback (name); } //American Greeting Method Public voidEnglishgreeting (stringname) {Console.WriteLine ("Hello,"+name); } //Chinese Greeting Method Public voidChinesegreeting (stringname) {Console.WriteLine ("Hello,"+name); } }}
Summarize:
- A delegate encapsulates a behavior that contains a special return type and a set of parameters, similar to an interface that contains a single method;
- The type signature described in the delegate type declaration determines which method can be used to create the delegate instance, while determining the signature of the call;
- In order to create a delegate instance, a method is required and (for instance methods) The target of the method is called;
- The delegate instance is not easy to change;
- Each delegate instance contains an invocation list---an action list;
- Delegate instances can be merged together, or another can be removed from one delegate instance;
- The event is not a delegate instance-----is just a paired Add/remove method (similar to a property's value method/assignment method)
One of the important features of C # Delegation