Commissioned
1 delegate return type delegate name ();
- Characteristics of the Commission
Similar to C + + function pointers, but it is type-safe, delegates allow methods to be passed as arguments, delegates can be used to define callback methods, and delegates can be chained together: for example, multiple methods can be called on an event.
The delegate determines which method is called at run time, and the delegate and method must have the same signature.
1 //Defining Delegates2 Public Delegate intCallback (intNUM1,intnum2);3 //Defining Method Classes4 classmathcalc{5 //Addition Method6 Public intPlus (intNumber1,intnumber2) {7 returnNumber1 +number2;8 }9 }Ten One Static voidMain (string[] args) { A //Delegate's Object - Callback Objcall; - //instance Mathcalc object of class theMathCalc MC =NewMathCalc (); - //associating a method with a delegate -Objcall =NewCallback (MC. Plus); - //You can use Callback Objcall = new Callback (MC. Plus); Declare delegates and associate with methods + //instantiate a delegate - intresult = Objcall (1,2); +System.Console.WriteLine ("The result is {0}", result); A}
Anonymous methods
The delegate can be called normally, and a separate method should be created to increase the coding overhead required to instantiate the delegate. To do this, you can use anonymous methods, which do not need to define the method that the delegate is referencing, and the method body of the method to be referenced as a code block that instantiates the delegate immediately after the delegate.
1 Delegate 2//3// semicolon not less
1 classprogram{2 //Defining Delegates3 Delegate intDelsum (intNUM1,intnum2);4 Static voidMain (string[] args) {5 intThird =3;6 //passing code as a delegate parameter7Delsum Deladd =Delegate(intFirstintsecond) {8 intsum = First +second;9 returnsum;Ten }; One intTotal = Deladd (1,2) +third; AConsole.WriteLine ("1+2+3="+Total ); - } -}
- Features of anonymous methods
The advantage of an anonymous method is that the overhead of the system is reduced, and the method is defined only when used by the delegate. Anonymous methods can help reduce the complexity of your code, but it is easy to confuse code if you have a lot of code for the delegate code block, which reduces the readability of your program.
You cannot use a jump statement to jump outside an anonymous method in an anonymous method, nor can a jump statement outside of an anonymous method jump to the inside of an anonymous method. Anonymous methods cannot access parameters that use the ref and out modifiers externally, but can access other variables declared externally.
Event
An event is a special delegate that, when something happens, an object can notify another object through an event.
- Defining events
- Subscribe to this event for an object
- Notifies subscribers of events that occur
1 Public classMeltdowneventargs:eventargs {2 Private stringmessage;3 PublicMeltdowneventargs (stringmessage) {4 This. Message =message;5 }6 Public stringMessage {7 Get {8 returnmessage;9 }Ten } One } A - Public classReactor { - Private inttemperature; the //Defining Delegates - Public Delegate voidMeltdownhandler (Objectreactor, Meltdowneventargs MEA); - //Defining Events - Public EventMeltdownhandler Onmeltdown; + Public intTemperature { - Set { +Temperature =value; A //when the temperature value is above 1000, a Meltdowneventargs object called Mymea is created, and the string is passed in to the message property of Mymea, and then the Onmeltdown event is generated. Pass the current reactor object and Mymea as parameters to Onmeltdown at if(Temperature > +){ -Meltdowneventargs Mymea =NewMeltdowneventargs ("Reactor meltdown in progress!"); -Onmeltdown ( This, Mymea); - } - } - } in } - to Public classReactormonitor { + //Reactormonitor constructor receives reactor object Myreactor as parameter - Publicreactormonitor (Reactor myreactor) { the //the constructor monitors the Myreactor object for the Onmeltdown event. When the Myreactor object produces an Onmeltdown event, the DisplayMessage () method is called to handle the event *Myreactor.onmeltdown + =NewReactor.meltdownhandler (displaymessage); $ }Panax Notoginseng Public voidDisplayMessage (ObjectMyreactor, Meltdowneventargs Mymea) { - Console.WriteLine (mymea.message); the } + } A the Public Static voidMain (string[] args) { +Reactor Myreactor =NewReactor (); -Reactormonitor Myreactormonitor =NewReactormonitor (myreactor); $ $Console.WriteLine ("Setting reactor temperature to degrees centigrade"); - //set the temperature value to -Myreactor.temperature = -; theConsole.WriteLine ("Setting reactor temperature to degrees centigrade"); - //set the temperature value toWuyiMyreactor.temperature = -; the}
Event handling mechanism in C #