Delegate delegate in C #

Source: Internet
Author: User

Delegates are also called delegates , representing a class of methods. This class of methods has the same modifiers, formal parameters, and return value types .

1. The delegate is both a method and a delegate is declared as follows: Add the delegate [' d?l?ɡ?t] keyword after the modifier

1  Public Delegate void Del (string message);

From the format, it is very similar to the declaration of the method, that is, more than a delegate keyword. It represents a method that declares a class that has a modifier of public, a string-type parameter, and no return value.

2. But the delegate is not an ordinary method, because it is also a class, so the delegate can only be declared, but not defined, such as cannot write:

private delegate void Doing( string name){        System.Windows.MessageBox.Show(); }

3. Instantiating a delegate, invoking a delegate

1 //declares a void, and the parameter is a delegate of string2  Public Delegate voidDel (stringname);3  4 //define a cut method5  Public voidCutstringname)6 {7System.Windows.MessageBox.Show ("Fuck"+name);8 }9 //instantiate the delegate, pass the parameter is a method of the name, here is the cutTenDel A =NewDel (cut); OneA"CCC");//call the delegate, which is equivalent to calling cut ("CCC");

4. What is the use of the Commission and what does it mean to be so complicated?

The method name is passed as a method parameter, which improves the variability and flexibility of the code; The delegate is used primarily for events and callback functions.

5. Delegated multicast (multicasting of a Delegate) operation "+ =", "-="

1 usingSystem;2  3 Delegate intNumberchanger (intn);4 namespaceDELEGATEAPPL5 {6    classtestdelegate7    {8       Static intnum =Ten;9        Public Static intAddnum (intp)Ten       { Onenum + =p; A          returnnum; -       } -   the        Public Static intMultnum (intq) -       { -Num *=Q; -          returnnum; +       } -        Public Static intGetnum () +       { A          returnnum; at       } -   -       Static voidMain (string[] args) -       { -          //Create a delegate instance - Numberchanger NC; inNumberchanger NC1 =NewNumberchanger (addnum); -Numberchanger NC2 =NewNumberchanger (multnum); toNC =nc1; +NC + = NC2;//here, the NC actually binds 2 delegates, which are executed in the order in which they are bound . -          //Call Multicast thenc5); *Console.WriteLine ("Value of Num: {0}", Getnum ()); $ Console.readkey ();Panax Notoginseng       } -    } the}

Note : Delegate objects can be merged using the "+ =" operator. A merge delegate invokes the two delegates it merges. Only delegates of the same type can be merged.

The "-=" operator can be used to remove a component delegate from a merged delegate.

No matter how far you go, looking back is also necessary. ********************************************************

Delegate delegate in C #

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.