This chapter discusses callback functions. The callback function is a very useful programming mechanism that has existed for many years.
The Microsoft. Net Framework provides a mechanism for callback functions through delegation (delegate).
Columns such as: Delegates ensure that callback methods are type-safe. Delegates also allow sequential calls to multiple methods and support the invocation of static and instance methods.
Delegates in C # can use them to invoke different functions while the program is running.
For a simple example, you are programming, you are now writing an ASP. js is unfamiliar to you, so you entrust a colleague of yours to help you complete the JS section. This is the Commission to give to others what you cannot do.
1. Simple Delegate http://www.cnblogs.com/birdshover/archive/2008/01/07/1029471.html
So what information does the delegate need to host? First it stores the method name, the parameter list (method signature), and the return type, such as:
Delegate string/* return type */processdelegate (Int32 i);
The blue part is the keyword that declares the delegate, Red is the return type, the black part is the type name of the delegate, and () is the parameter part. You have to use this delegate to do things, you have to satisfy the condition:
- The return type is the same as the return type of the delegate, and here is the string type
- Parameter list can have only one parameter and is Int32 type
For example:
The result of the output is: text1tex2
Public delegate String Processdelegate (string s1, string s2);
Class Program
{
static void Main ()
{
To invoke the process method using delegate Processdelegate
Processdelegate PD = new Processdelegate (new Test (). Process);
Console.WriteLine (PD ("Text1", "Text2"));
}
}
public class Test
{
public string Process (string s1, string s2)
{
return s1 + s2;
}
}
2. Callback function
The callback function is to pass a method to another method to execute. It differs from the delegate in that its method parameters, the return value can be the parameters of the caller, and the return value can be different.
Output Result:
Text1text2
Text1
Text2
Text2text1
Public delegate String Processdelegate (string s1, string s2);
Class Program
{
static void Main ()
{
Test T = new Test ();
The process method (caller) invokes a callback function, PROCESS1, which only executes the callback function.
As you can see, any method that conforms to this delegate can be passed in, meaning that this part of the code is mutable.
Pass the Process1 2 3 method to the process method to execute
string r1 = t.process ("Text1", "Text2", New Processdelegate (T.PROCESS1));
string r2 = t.process ("Text1", "Text2", New Processdelegate (T.PROCESS2));
String r3 = t.process ("Text1", "Text2", New Processdelegate (T.PROCESS3));
Console.WriteLine (R1);
Console.WriteLine (R2);
Console.WriteLine (R3);
}
}
public class Test
{
public string process (string s1, string s2, processdelegate process)
{
return process (S1, S2);
}
public string Process1 (string s1, string s2)
{
return s1 + s2;
}
public string Process2 (string s1, string s2)
{
return s1 + Environment.NewLine + s2;
}
public string Process3 (string s1, string s2)
{
return s2 + s1;
}
}
17.1 Initial knowledge entrusted
The following code demonstrates how to declare, create, and use a delegate:
Using System;
Using System.Windows.Forms;
Using System.IO;
Namespace WindowsFormsApplication1
{
Declares a delegate type whose instance refers to a method
Specifies the signature of a callback function that gets a Int32 parameter that returns void
Internal delegate void Feedback (Int32 value);
Public sealed class Program
{
public static void Main ()
{
Staticdelegatedemo ();
Instancedelegatedemo ();
ChainDelegateDemo1 (new program ());
ChainDelegateDemo2 (new program ());
}
public static void Staticdelegatedemo ()
{
Console.WriteLine ("----Static Delegate Demo----");
Counter (1, 3, NULL);
Prefix program optional
Counter (1, 3, new Feedback (Program.feedbacktoconsole));
Counter (1, 3, new Feedback (Feedbacktomsgbox));
Console.WriteLine ();
}
private static void Instancedelegatedemo ()
{
Console.WriteLine ("----Instance Delegate Demo----");
Program P = new program ();
Counter (1, 3, new Feedback (P.feedbacktofile));
Console.WriteLine ();
}
private static void ChainDelegateDemo1 (program P)
{
Console.WriteLine ("----Chain Delegate Demo 1----");
Feedback FB1 = new Feedback (feedbacktoconsole);
Feedback FB2 = new Feedback (Feedbacktomsgbox);
Feedback fb3 = new Feedback (p.feedbacktofile);
Feedback fbchain = null;
Fbchain = (Feedback) delegate.combine (Fbchain, FB1);
Fbchain = (Feedback) delegate.combine (Fbchain, FB2);
Fbchain = (Feedback) delegate.combine (Fbchain, FB3);
Counter (1, 2, fbchain);
Console.WriteLine ();
Fbchain = (Feedback) delegate.remove (Fbchain, New Feedback (Feedbacktomsgbox));
Counter (1, 2, fbchain);
}
private static void ChainDelegateDemo2 (program P)
{
Console.WriteLine ("----Chain Delegate Demo 2----");
Feedback FB1 = new Feedback (feedbacktoconsole);
Feedback FB2 = new Feedback (Feedbacktomsgbox);
Feedback fb3 = new Feedback (p.feedbacktofile);
Feedback fbchain = null;
Fbchain + = FB1;
Fbchain + = FB2;
Fbchain + = FB3;
Counter (1, 2, fbchain);
Console.WriteLine ();
Fbchain-= new Feedback (Feedbacktomsgbox);
Counter (1, 2, fbchain);
}
private static void Counter (Int32 from, Int32 to, Feedback FB)
{
for (Int32 val = from, Val <= to; val++)
{
If any callback functions are specified, they are called
if (FB! = null)
FB (Val);
}
}
private static void Feedbacktoconsole (Int32 value)
{
Console.WriteLine ("item=" + value);
}
private static void Feedbacktomsgbox (Int32 value)
{
MessageBox.Show ("item=" + value);
}
private void Feedbacktofile (Int32 value)
{
StreamWriter SW = new StreamWriter ("Status", true);
Sw. WriteLine ("item=" + value);
Sw. Close ();
}
}
}
17.2 using a delegate callback static method
In the Staticdelegatedemo method, when the counter method is called for the first time, NULL is passed for the third argument. Because counter's FB parameter receives NULL, the callback function is not invoked when each data item is processed.
The Staticdelegatedemo method then calls the counter method again, passing a newly constructed feedback delegate object for the third parameter. The delegate object (the new operator, the newly created feedback object) is a wrapper (wrapper) of the method that allows the method to be called back indirectly through the wrapper.
In this example, the full name of the static method Program.feedbacktoconsole is passed to the constructor of the feedback delegate type. Shows that Feedbacktoconsole is the way to wrap. The reference returned by the new operator is passed as the third argument to counter.
In one type, when a private member of another type can be invoked through a delegate, there is no problem if the delegate object is created with sufficient security and accessibility code.
All operations in this example are type-safe. For example, when constructing a feedback delegate object, the compiler ensures that the signature of the program's Feedbacktoconsole method is compatible with the signature of the feedback delegate definition. Specifically, Feedbacktoconsole must obtain a parameter, and both must have the same return type (void).
When you bind a method to a delegate, both C # and the CLR allow reference types for covariance and contravariance.
17.3 using a delegate callback instance method
A program object named P is constructed in Instancedelegatedemo. This program object does not define any instance fields and properties. The constructor for the counter delegate type is passed P.feedbacktofile, which causes the delegate to wrap a reference to the Feedbacktofile method, which is an instance method, not a static method. The Feedbacktofile instance method is called when counter invokes the callback function identified by its FB argument.
17.4 Delegate Disclosure
On the surface, delegates seem to be easy to use: Use the delegate keyword in C # to construct a delegate instance with the familiar new operator.
The CLR and the compiler do a lot of work to hide the complexity of delegates.
17th Chapter Commission