委托是一个类,它定义了方法的类型,使得方法可以当作另一个方法的参数传递,同时可以在另一个方法中调用。
事件是一种特殊的委托。
1、委托的声明
(1)delegate
我们经常使用的一种声明方式。
delegate 至少0个参数,至多32个参数,可以无返回值,可以指定返回值类型。
例如:public delegate int MethodDelegate(int x,int y); 表示两个int参数,并且返回值是int
(2)Action
Action是无返回值的范型委托。
Action至少0个参数,至多16个参数,并且无返回值。
Action表示无参数,无返回值的委托。
Action<string>表示传入参数string,无返回值的委托。
Action<string,string,int>表示传如参数string,string,int,无返回值的委托。
例如:
public Test<T>(Action<T> action,T param)
{
action(param);
}
(3)Func
Func表示有返回值的泛型委托。
Func至少0个参数,至多16个参数,根据返回值泛型返回,必须有返回值,不可以为void。
Func<int> 表示无参数,返回值类型是int的委托。
Func<object,string,int>表示参数是object,string,返回值类型是int的委托。
Func<T1,T2,T3,int>表示参数是T1,T2,T3(泛型),返回值类型是int的委托。
例子:
public int Test<T1,T2>(Func<T1,T2,int> func,T1 t1,T2 t2)
{
return func(t1,t2);
}
(4)Predicate
Predicate是返回值是bool类型的委托。
Predicate仅有一个传入参数,并且返回值类型固定是bool的委托。
Predicate<int>表示传入参数是int,并且返回值类型是bool的委托。
2、委托的使用
(1)Delegate的使用
public class Program
{
/// <summary>
/// define delegate prototype
/// </summary>
/// <param name="x">int param</param>
/// <param name="y">int param</param>
/// <returns>int</returns>
public delegate int MethodDelegate(int x, int y);
/// <summary>
/// define delegate variable
/// </summary>
private static MethodDelegate _methodDelegate;
private static void Main(string[] args)
{
//Instantiate delegate to reference Add func
_methodDelegate = Add;
//Use delegate instance to call func
Console.WriteLine( _methodDelegate.Invoke(10, 20));
Console.ReadKey();
}
/// <summary>
/// define function
/// </summary>
/// <param name="x">int param</param>
/// <param name="y">int param</param>
/// <returns>int</returns>
private static int Add(int x, int y)
{
return x + y;
}
}
(2) Action的使用
public class Program
{
private static void Main(string[] args)
{
Test<string>(DoAction,"Hello World");
Test<int>(DoAction,10000);
Test((p)=>{Console.WriteLine("Lambda Expression"+" "+ p);},"Hello");
Console.ReadKey();
}
public static void Test<T>(Action<T> action, T param)
{
action(param);
}
private static void DoAction(string str)
{
Console.WriteLine(str);
}
private static void DoAction(int index)
{
Console.WriteLine(index);
}
}
可以使用Action<T1,T2,T3,T4>委托以参数形式传递方法,而不用显示声明自定义的委托。封装的方法必须与此委托定义的方法相同。也就是说,封装的方法必须有四个均通过值传递给他的参数,并且无返回值。在C#中该方法必须返回void,通常这种方法用于执行某个操作。
(3)Func的使用
public class Program
{
private static void Main(string[] args)
{
Console.WriteLine(AddContent(DoAdd,"Jack","Leo"));
Console.ReadKey();
}
private static string AddContent<T1, T2>(Func<T1, T2, string> func, T1 t1, T2 t2)
{
return func(t1, t2);
}
private static string DoAdd(string t1, string t2)
{
return t1 + " " + t2;
}
}
(4)Predicate
泛型委托,表示定义一组条件并确定对象是否符合这些条件的方法。此委托由Array与List类的几个方法使用,用于在集合中搜索数据。
public class Program
{
private static void Main(string[] args)
{
double[] locationList = {1.1, 1.21, 21, 11.1};
var first = Array.Find(locationList, LocationGt10);
Console.WriteLine(first);
Console.ReadKey();
}
private static bool LocationGt10(double location)
{
return location > 10.0;
}
}
3、委托的清空
(1)在类中声明清空委托方法,依次循环去除委托
public MethodDelegate OnDelegate;
public void ClearDelegate()
{
while (this.OnDelegate != null)
{
this.OnDelegate -= this.OnDelegate;
}
}
(2)如果在类中没有申明清空委托的方法,我们可以利用GetInvocationList查询出委托引用,然后进行去除。
public MethodDelegate OnDelegate;
static void Main(string[] args)
{
Program test = new Program();
if (test.OnDelegate != null)
{
System.Delegate[] dels = test.OnDelegate.GetInvocationList();
for (int i = 0; i < dels.Length; i++)
{
test.OnDelegate -= dels[i] as MethodDelegate;
}
}
}
4、委托的特定
委托类似与C++的函数指针,但是他们是类型安全的。
委托允许方法作为参数进行传递。
委托可用于定义回调方法。
委托可以连接在一起。例如:可以对一个事件调用多个方法。
方法不必与委托签名完全匹配。
5、总结
Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型
Func可以接受0个至16个传入参数,必须具有返回值
Action可以接受0个至16个传入参数,无返回值
Predicate只能接受一个传入参数,返回值为bool类型
详细参考:
http://www.cnblogs.com/akwwl/p/3232679.html
http://www.fengfly.com/plus/view-209140-1.html
http://www.cnblogs.com/foolishfox/archive/2010/09/16/1827964.html
https://msdn.microsoft.com/en-us/library/bb549151(v=vs.110).aspx