01、简单的委托
那委托需要承载哪些信息呢?首先,它存储了方法名,还有参数列表(方法签名),以及返回的类型。比如:
delegate string/*返回类型*/ ProcessDelegate(int i);
这就是一个委托的定义。蓝色部分是声明委托的关键字,红色部分是返回的类型,而黑色部分是委托的类型名,和一个类名差不多,而()里的就是参数部分。它的意思是,你要使用这个委托来做事情的话,那么,做事情的方法必须满足以下条件:
1、返回类型和委托的返回类型一致,这里是string类型;
2、能且只能有一个参数,并且是int类型。
OK,满足以上两个条件,一切就可以工作了:)
using System;
using System.Collections.Generic;
using System.Text;
namespace TestApp
{
/// <summary>
/// 委托
/// </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)
{
/* 调用方法 */
ProcessDelegate pd = new ProcessDelegate(new Test().Process);
Console.WriteLine(pd("Text1", "Text2"));
}
}
public class Test
{
/// <summary>
/// 方法
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public string Process(string s1, string s2)
{
return s1 + s2;
}
}
}
using System.Text;
namespace TestApp
{
/// <summary>
/// 委托
/// </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)
{
/* 调用方法 */
ProcessDelegate pd = new ProcessDelegate(new Test().Process);
Console.WriteLine(pd("Text1", "Text2"));
}
}
public class Test
{
/// <summary>
/// 方法
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public string Process(string s1, string s2)
{
return s1 + s2;
}
}
}
02、泛型委托
using System;
using System.Collections.Generic;
using System.Text;
namespace TestApp
{
/// <summary>
/// 委托
/// </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)
{
/* 调用方法 */
ProcessDelegate<string,int> pd = new ProcessDelegate<string,int>(new Test().Process);
Console.WriteLine(pd("Text1", 100));
}
}
public class Test
{
/// <summary>
/// 方法
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public string Process(string s1,int s2)
{
return s1 + s2;
}
}
}
using System.Text;
namespace TestApp
{
/// <summary>
/// 委托
/// </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)
{
/* 调用方法 */
ProcessDelegate<string,int> pd = new ProcessDelegate<string,int>(new Test().Process);
Console.WriteLine(pd("Text1", 100));
}
}
public class Test
{
/// <summary>
/// 方法
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public string Process(string s1,int s2)
{
return s1 + s2;
}
}
}
03、回调方法
using System;
using System.Collections.Generic;
using System.Text;
namespace TestApp
{
/// <summary>
/// 委托
/// </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)
{
/* 调用方法 */
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;
}
}
}
using System.Text;
namespace TestApp
{
/// <summary>
/// 委托
/// </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)
{
/* 调用方法 */
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;
}
}
}