Commissioned by 1.
The delegate declaration prototype is delegate < function return type > < delegate name > (< function parameter >) for example: public delegate void Calculator(int I, int j); This defines a delegate, but a delegate is the equivalent of declaring a class in.NET, and many functions cannot be used unless a class is instantiated as an object, nor can delegates.
Now, let’s go straight to defining a delegate and using it.
namespace _delegate { public delegate void Calculator(int i, int j); // Define the delegate Compute, with two parameters I and j; Method signatures are the same as addition and subtraction. All it does is add the delegate keyword. Public class DelegateCal {private static void Add(int I, int j) {console. WriteLine("{0}", I + j); } private static void Sub(int I, int j)} private static void Sub(int I, int j) {console. WriteLine("{0}", i-j); } Private static void Calculator(int I, int j, Calculator Cal)//Calculator This method takes three parameters. The third Compute is the delegate type defined earlier. { Cal(i, j); // delegate type Compute defines a Cal variable. Dynamically deciding which method to use when assigning a value to Cal} // delegate is a class that defines the type of method so that it can be passed as an argument to another method. /*static void Main(string[] args) { Calculator(2, 5, Add); Calculator(5, 2, Sub); Console.ReadLine(); }*/ / Compute and int are data types, so we can define two variables and assign methods to them. Static void Main(string[] args) {// bind method to delegate /*Compute delegate1, delegate2; delegate1 = Add; delegate2 = Sub; The Calculator (1, 2, delegate1); / / 3 Calculator (7, 4, delegate2); //3 Console.ReadLine(); */ // A delegate has another feature that differs from an int or other data type in that it can bind multiple methods to the same delegate. /*Compute delegate1; delegate1 = Add; delegate1 += Sub; Calculator(5, 1, delegate1); Console.ReadLine(); // add 6, subtract 4; In fact, instead of Calculator, we can call Add or Sub directly through the delegate. Calculator Delegate1; delegate1 = Add; delegate1 += Sub; delegate1(9, 2); Console.ReadLine(); // And = 11, difference = 7}}}Copy the code
2. The event
Now that we know about delegates, let’s talk about events. The definition of an event in C# is very similar to the declaration of a delegate: event < delegate type > event name for example: public event Computer OnComputer; // Define the event. After declaring the event with the proxy binding, you can instantiate the event and register the function into the event. Here’s an example:
namespace _delegate { public delegate void Computer(); // First declare a delegate. public class EventCal { public event Computer OnComputer; Public virtual void FireEvent() {if (OnComputer! = null) { OnComputer(); Public void EventHandler() public void EventHandler() {int a = 1; int b = 1; int c = a + b; Console.WriteLine("a+b={0}",c); } public static void Main() { EventCal ec = new EventCal(); Compere c = new Compere(); ec.OnComputer += new Computer(c.EventHandler); / / registered ec FireEvent (); Console.Read(); }}}Copy the code
The EventCal class acts as a time publisher and defines information about the event, including an event for calculation (OnComputer) and a function that fires the event (FireEvent()).
The Compere class acts as an event subscriber, or moderator. It defines an event handler (EventHandle()) and associates it with the event with +=. So that when the event fires I can call my method, which is essentially evaluating the value of a plus b. Note that it is important that the event handler’s method signature be the same as the proxy’s method signature
namespace _delegate { public delegate void Computer(object sender, MyEventArgs e); Public class EventCal {public event Computer OnComputer; Public void FireEvent(MyEventArgs e) public void FireEvent(MyEventArgs e) {if (OnComputer! = null) { OnComputer(this,e); }}} public class MyEventArgs: EventArgs {public readOnly int _a,_b; Public MyEventArgs(int a,int b)// This is a constructor,int a,int b {this._a = a; this._b = b; }} public void EventHandler(object sender,MyEventArgs e) { Console.WriteLine("a+b={0}",e._a+e._b); } public static void Main() { EventCal ec = new EventCal(); Compere c = new Compere(); MyEventArgs e = new MyEventArgs(1, 1); // Constructor argument ec.oncomputer += new Computer(c.eventhandler); / / registered ec FireEvent (e); Console.Read(); }}}Copy the code
This is an event with parameters.