One of the important features of C # Delegation

Source: Internet
Author: User

The composition of a delegate must meet the 4 conditions:

    1. declaring the delegate type;
    2. There must be a method that contains the code to be executed;
    3. A delegate instance must be created;
    4. 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

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.