Commissioned:
Pass the method as a parameter
public delegate void AddDelegate (string name);
public class ad{
AddDelegate is an example of a delegate, passing the method as a parameter, logically separating, decoupling
public static void Addhander (String name,adddelegate adddelegate) {
AddDelegate (name);//Call this method
}
}
public static void Add (string name) {CW (name);}
Call:
AddDelegate add = new AddDelegate (ad.add);//Instantiation of Delegates
Ad.addhander ("ABC", add);//The delegate is passed in as a parameter
Event:
public delegate void Catdelegate ();//define a delegate
Declaring a cat
public class cat{
Public Event catdelegate Cathander ;//Declaring an event
public void Miaocat () {
Cathander.invoke ();//Execute Event
}
}
Definition: A delegate is a type, and an event is an instance of a delegate;
Call: Cat Cat =new Cat ();
cat.cathander+=run;//Register Action Run () method
cat.cathander+=miao;//Register Action Miao () method
Cat. Miaocat ();//Execute Call event method, the procedure executes run, Miao these two methods
C#_ Delegates and Events