The creation rule for an event is that the type must be a delegate type, that is, the delegate instantiates the event
The case code is as follows:
Here are the two methods to be delegated later:
Public classEventManager {//The first method Public voidADD (intIintj) {intres = i +J; MessageBox.Show (Res. ToString ()); } //a second method Public voidSayHello (intIintj) {MessageBox.Show ("Hello Word"+ (i*j). ToString ()); } }
This is the creation of the delegate and the time, including the multicast delegate, each time a new delegate is created to be placed on the event, a new delegate is created, and a delegate queue is generated:
//Create a delegate Public Delegate voidNum (intIintj); //depending on the delegate instantiation event, the event here only needs to be instantiated once Public EventNum caculate; Private voidButton1_Click (Objectsender, EventArgs e) {EventManager em=NewEventManager (); //using the same implementation method as the delegate overload parameternum num =NewNum (em. ADD); //using the event binding delegate Add methodCaculate + =num; //using event binding the second way to say HellowordCaculate + =NewNum (em. SayHello); //in this case, only the first parameter is required for the event, and the parameters of the event are passed into each delegate's method to perform a different result. This. Caculate (5,5); }
Multicast delegates and Events