01. Simple Delegation
What information does the delegate need to carry? First, it stores the method name, the parameter list (method signature), and the returned type. For example: Delegate string/* return type */processdelegate (int I); this is the definition of a delegate. The blue part is the keyword for declaring the delegate, the red part is the returned type, and the black part is the delegate type name, which is similar to a class name, while the () part is the parameter part. It means that if you want to use this delegate to do things, the method of doing things must meet the following conditions: 1. The return type is the same as the return type of the delegate, here is the string type; 2. It can have only one parameter and is of the int type.
OK. If the above two conditions are met, everything can work :)
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace testapp
{
/// <Summary>
/// Delegate
/// </Summary>
/// <Param name = "S1"> </param>
/// <Param name = "S2"> </param>
/// <Returns> </returns>
Public Delegate string processdelegate (string S1, string S2 );
Class Program
{
Static void main (string [] ARGs)
{
/* Call Method */
Processdelegate Pd = new processdelegate (new test (). process );
Console. writeline (Pd ("text1", "text2 "));
}
}
Public class test
{
/// <Summary>
/// Method
/// </Summary>
/// <Param name = "S1"> </param>
/// <Param name = "S2"> </param>
/// <Returns> </returns>
Public String process (string S1, string S2)
{
Return S1 + S2;
}
}
}
02. Generic Delegation
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace testapp
{
/// <Summary>
/// Delegate
/// </Summary>
/// <Param name = "S1"> </param>
/// <Param name = "S2"> </param>
/// <Returns> </returns>
Public Delegate string processdelegate <t, s> (T S1, s S2 );
Class Program
{
Static void main (string [] ARGs)
{
/* Call Method */
Processdelegate <string, int> Pd = new processdelegate <string, int> (new test (). process );
Console. writeline (Pd ("text1", 100 ));
}
}
Public class test
{
/// <Summary>
/// Method
/// </Summary>
/// <Param name = "S1"> </param>
/// <Param name = "S2"> </param>
/// <Returns> </returns>
Public String process (string S1, int S2)
{
Return S1 + S2;
}
}
}
03. Callback Method
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace testapp
{
/// <Summary>
/// Delegate
/// </Summary>
/// <Param name = "S1"> </param>
/// <Param name = "S2"> </param>
/// <Returns> </returns>
Public Delegate string processdelegate (string S1, string S2 );
Class Program
{
Static void main (string [] ARGs)
{
/* Call Method */
Test T = new test ();
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;
}
}
}