C # delegate, I looked at the information and posts for a long time,
The previous understanding of a delegate is that the delegate is actually passing a method as a parameter to the first method.
Now the recognition of the delegate has changed some, the delegate can achieve:
1. As a bridge between two methods that cannot be called directly
2. When you do not know the specific implementation of the time to apply the delegate (such as event)
When using a delegate, two steps are required:
1. define a delegate as if it were a class defined;
2. instantiate one or more of the delegates .
3. for the delegate registration method
when a delegate is defined, it is often created outside of a class and is public (equivalent to a global variable, because the purpose of declaring a delegate is to expose it to the client of the class for method registration , if it is private, the client is not visible at all, it is useless )
Let's use the example to describe the usage of the delegate
First Instance:
The scene is like this: 1. I asked my friend and friend's girlfriend out to play 2. I asked my friend out to play with her girlfriend.
This scene (we see the second kind) I do not have the authority to call a friend's girlfriend out to play!!! (You know), but we can entrust a friend to ask him to call his girlfriend out.
The code is as follows
<span style= "White-space:pre" ></span> #region myownexmple public delegate void Delegate_exmple (); public static void Testexmple () {Me me = new Me (); Me. Name = "Richard" My Name ";" Friend F = new friend (); F.name = "Neil" My friend's name ";" F.gfname = "Leo" My friend's girlfriend's name ";" Me does not have permission called f GF me.delegate2 = f.callgf;//Registration Method Me.callfriend (f); } public class Me {public delegate_exmple delegate2; public string Name {get; set;} public void Callfriend (Friend f) {Console.WriteLine (Name + "I must have come first"); Console.WriteLine (F.name + "has come"); if (delegate2! = null) {Delegate2 (); }}} public class Friend {public string Name {get; set;} public string Gfname {get; set;} public void CalLGF () {Console.WriteLine (Gfname + "has come"); }} #endregion
A second example:
Observer pattern (divided into two types: 1. Delegate only (in each region, the part that is unregistered) 2. Combine delegates and events)
<span style= "White-space:pre" ></span> #region Observer mode public delegate void Delegate_observe (int temperatur e); public static void Testobserve () {Heater heater = new heater (); #region delegate If you use a delegate, you must write the delegate outside (similar to a global variable)//heater.delegate1 = new Alarm (). Makealert; Heater.delegate1 + = new Monitor (). ShowMessage; Heater. Boil (); #endregion #region Event Heater.eventtellother + = (new Alarm ()). makealert;//Anonymous delegate Heater.eventtellother + = new Monitor (). ShowMessage; Heater. Boil ();//Register method to execute #endregion}//Observer mode water heater is commissioned (so to add a delegate within the water heater method) to tell the alarm and the monitor to do something public class H Eater {public int temperature {get; set;} #region use delegates, but global variables//public delegate_observe delegate1 are required; #endregion #region Use the event public delegate void Tellother (int temperature); Public event Tellother Eventtellother; #endregion public void Boil () {for (int i = 0; I <=; i++) { temperature = i; if (temperature% = = 0) {#region Use delegate, but global variable//if required ( Delegate1! = NULL)//If there is a method to register a delegate variable//Lenovo to the fact that the delegate definition is a global variable that is only instantiated anywhere, and the method is registered for it {//Delegate1 (temperature); To invoke a method through a delegate//} #endregion #region Use Event if (eventtellother! = null) {Eventtellother (temperature); } #endregion}}} public class Alarm {public void Makealert (int temperature) {Console.writeli NE ("Water temperatureAlready {0} degrees, making a chirping sound ~ ", temperature); }} public class Monitor {public void showmessage (int temperature) { Console.WriteLine ("The water temperature has been {0} degrees, showing the information", temperature); }} #endregion
From the first example, we can feel a little bit about the benefits of a delegate. But the helpless contact after not much, can not give a better example, hope you Daniel pointing.
C # delegates and events