A delegate (delegate) is a type that can store a reference as a function, similar to a function pointer in C + +.
callback function
a callback function in C + + is implemented using a function pointer. Similarly, C # uses delegates to implement the functions of callback functions.
Why is a callback function called a callback function? For example, if you call a function, it's called, but if you're calling a function to give it a function to call your function, then the function you provide is called the callback function (callback).
For a dynamic language like Python, there is no c#,c++ to provide a special syntax implementation callback function, because in Python, a function is also an object, no matter when the argument is passed or when the return value of the function.
The callback function is also used as a "plugin":
In C + + STL library, the specific algorithm of the sort function has been implemented, but the concrete comparison method of two elements is provided by the callback function (comparison function) to ensure the algorithm can be used for different types of int,string.
void sort (iterator start, iterator end);
void sort (iterator start, iterator end, strictweakordering CMP);
C # delegates
delegates are often used in event handling. Because you can delegate the specific actions (what to do) after the event is triggered to the implementation class. This is the Hollywood rule, "Don t call me, I'll call you".
This example comes from the introduction of the C # Classic:
Delegate Double processdelegate (double param1, double param2);
Static double Multiply (double param1, double param2)
{return
param1 * param2;
}
STAITC double Divide (double param1, double param2)
{return
param1/param2;
}
if (input = = "M")
process = new Processdelegate (Multiply);
else
process = new Processdelegate (Divide);
But the truth is that we do not initialize the callback function by logical judgment, and the following example of this function plug-in is more common.
static void ExecuteFunction (Processdelegate process)
{
process (2.2, 3.3);
}
instance of C # Delegate implementation callback:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Threading; namespace Stockes {public partial class Callback:form {public CallBack () {InitializeComponent
();
Initialization callback Method WriteBoxCallback1 = new WriteBoxCallback1 (Write1);
writeBoxCallbacK2 = new WriteBoxCallback2 (WRITE2);
}//declares that the delegate has implemented a callback mechanism private delegate void Writetextbox (char ch);
Private Writetextbox Writetextbox;
TEXT1 callback Private delegate void WriteBoxCallback1 (char ch);
Private WriteBoxCallback1 WriteBoxCallback1;
TEXT2 callback Private delegate void WriteBoxCallback2 (char ch);
Private WriteBoxCallback2 writeBoxCallbacK2; private void Button1_Click (object sender, EventArgs e) {if (checkbox1.checked) {Groupbox4.te XT = "Running ..."
"; GroupBox4.Refresh ();
Textbox1.clear ();
Textbox1.refresh (); Thread Th1 = new Thread (new ThreadStart (dowrite1));//Create Threads 1 Th1.
Start ()//start thread 1} if (checkbox2.checked) {Groupbox2.refresh (); Groupbox5.text = "Running ...
";
Groupbox5.refresh ();
Textbox2.clear ();
Textbox2.refresh (); Thread Th2 = new Thread (new ThreadStart (DOWRITE2));//Create Threads 2 Th2. Start ()//Start Thread 2}}//TEXT1 uses callback statements private void CallTex1 (char ch) {Textbox1.invoke (write
BOXCALLBACK1,CH);
//TEXT2 use callback statements private void CallText2 (char ch) {Textbox2.invoke (writeboxcallback2,ch);
///Use delegate private void Writetex (Writetextbox write) {string str = TextBox3.Text.Trim (); for (int i = 0; i < str. Length;
i++) {write (str[i]);
DateTime now = DateTime.Now; while (now. AddSeconds (1) > DateTime.Now) {}}
//text1 add value private void Write1 (char ch) {textbox1.appendtext (ch+ "\ r");
//text2 add value private void Write2 (char ch) {textbox2.appendtext (ch+ "\ r"); }//thread 1 called method private void Dowrite1 () {if (checkbox1.checked) {writetextbox = new W
Ritetextbox (CALLTEX1);
Writetex (Writetextbox); The method called by thread 2 is private void Dowrite2 () {if (checkbox2.checked) {Writetextbox
= new Writetextbox (CALLTEXT2);
Writetex (Writetextbox);
}
}
}
}