C # One of the important features of delegation,

Source: Internet
Author: User

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)

 

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.