C # Programming: an example of a delegate program

Source: Internet
Author: User

A delegate is not a method, it is a special type that is used to call a method that has the same signature as the delegate (the signature here refers to the parameter list of the method).

An important feature of a delegate is that when a delegate calls a method, it does not have to care about the type of object that the method belongs to, it only requires that the signature of the provided method matches the signature of the delegate.

Delegate declaration format: Modifier delegate return type delegate name (parameter list)

public delegate void Btevent ();

An example is attached:

Using system;using system.collections.generic;using system.linq;using system.text;namespace Delegate {public class ZhangXiao San {//actually the ticket is the small Zhang public static void Buyticket () {Console.WriteLine ("Buy Ticket!        "); } public static void Buymovieticket () {Console.WriteLine ("Buy movie tickets!        "); } public static void Buystocksticket () {Console.WriteLine ("Buy Stock!        ");        }} Class Program {//Declare a delegate//Delegate declaration format: Modifier delegate return type delegate name (parameter list) public delegate void Btevent ();            static void Main (string[] args) {Console.WriteLine ("=========== single Delegate ============");            Btevent mydelegate = new Btevent (zhangxiaosan.buyticket);            MyDelegate ();//Invoke Delegate Console.WriteLine ("=========== delegate merge ============");            When a delegate can be merged, the consolidation of the delegate is called multicast mydelegate + = Zhangxiaosan.buymovieticket;            MyDelegate + = Zhangxiaosan.buystocksticket; MyDelegate ();//CallEntrusted Console.WriteLine ("========== Cancel the entrustment =============");            MyDelegate-= Zhangxiaosan.buymovieticket;        MyDelegate ();//Call Delegate Console.readkey (); }    }}


C # Programming: an example of a delegate program

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.