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