C#委托的介绍(delegate、Action、Func、predicate)

委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递。事件是一种特殊的委托。

1.委托的声明

(1). delegate

delegate我们常用到的一种声明,自.net 2.0

Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。

例:
 

 1    public delegate int MethodDelegate(int x, int y);//表示有两个参数,并返回int型。
 2    private static MethodDelegate method;
 3    static void Main(string[] args)
 4    {
 5       method = new MethodDelegate(Add);
 6       Console.WriteLine(method(10,20));
 7       Console.ReadKey();
 8    }
 9 
10    private static int Add(int x, int y)
11    {
12       return x + y;
13    }

(2). Action

Action是无返回值的泛型委托。

Action 表示无参,无返回值的委托

Action<int,string> 表示有传入参数int,string无返回值的委托

Action<int,string,bool> 表示有传入参数int,string,bool无返回值的委托

Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托

Action至少0个参数,至多16个参数,无返回值。

例:
public void Test<T>(Action<T> action,T p)
{
action(p);
}

例:
 

 1     static void Main(string[] args)
 2     {
 3         Test<string>(Action,"Hello World!");
 4         Test<int>(Action, 1000);
 5         Test<string>(p => { Console.WriteLine("{0}", p); }, "Hello World");//使用Lambda表达式定义委托
 6         Console.ReadKey();
 7     }
 8     public static void Test<T>(Action<T> action, T p)
 9     {
10         action(p);
11     }
12     private static void Action(string s)
13     {
14         Console.WriteLine(s);
15     }
16     private static void Action(int s)
17     {
18         Console.WriteLine(s);
19     }


可以使用 Action<T1, T2, T3, T4> 委托以参数形式传递方法,而不用显式声明自定义的委托。 封装的方法必须与此委托定义的方法签名相对应。 也就是说,封装的方法必须具有四个均通过值传递给它的参数,并且不能返回值。 (在 C# 中,该方法必须返回 void)通常,这种方法用于执行某个操作。

pictureBox1.BeginInvoke(new Action(()=>this.pictureBox1.Image=new Bitmap("图片地址")));

Action委托没有什么特殊的,它相当于你定义这样一个委托:void delegate MyDelegate(); 无参数无返回值。

this.Invoke(new delegate { pictureBox1.Image = xxx; });
this.Invoke(new Action(() => pictureBox1.Image = xxx));
都是一样的

(3). Func

Func是有返回值的泛型委托,自.net3.5 新增

Func<int> 表示无参,返回值为int的委托

Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型)返回值为int的委托

Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void

例: 

  

 1     static void Main(string[] args)
 2     {
 3         Console.WriteLine(Test<int,int>(Fun,100,200));
 4         Console.ReadKey();
 5     }
 6     public static int Test<T1, T2>(Func<T1, T2, int> func, T1 a, T2 b)
 7     {
 8         return func(a, b);
 9     }
10     private static int Fun(int a, int b)
11     {
12         return a + b;
13     }


Func<T,TResult> 的表现形式分为以下几种:

1、Func<T,TResult>
2、Func<T,T1,TResult>
3、Func<T,T1,T2,TResult>
4、Func<T,T1,T2,T3,TResult>
5、Func<T,T1,T2,T3,T4,TResult>

分别说一下各个参数的意义,TResult表示
委托所返回值 所代表的类型, T,T1,T2,T3,T4表示委托所调用的方法的参数类型,

以下是使用示例:

 1     Func<int, bool> myFunc = null;//全部变量
 2 
 3     myFunc = x => CheckIsInt32(x);  //给委托封装方法的地方 使用了Lambda表达式
 4 
 5     private bool CheckIsInt32(int pars)//被封装的方法
 6     {
 7       return pars == 5;
 8     }
 9 
10     bool ok = myFunc(5);//调用委托

如果我们需要所封装的方法不返回值,就使用 Action

(4) .predicate

predicate 是返回bool型的泛型委托

predicate<int> 表示传入参数为int 返回bool的委托

Predicate有且只有一个参数,返回值固定为bool

例:public delegate bool Predicate<T> (T obj)
 
泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素。

 1     static void Main(string[] args)
 2     {
 3         Point[] points = { new Point(100, 200), 
 4         new Point(150, 250), new Point(250, 375), 
 5         new Point(275, 395), new Point(295, 450) };
 6         Point first = Array.Find(points, ProductGT10);
 7         Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
 8         Console.ReadKey();
 9     }
10     
11     private static bool ProductGT10(Point p)
12     {
13         if (p.X * p.Y > 100000)
14         {
15             return true;
16         }
17         else
18         {
19             return false;
20         }
21     }
22         


使用带有 Array.Find 方法的 Predicate 委托搜索 Point 结构的数组。如果 X 和 Y 字段的乘积大于 100,000,此委托表示的方法 ProductGT10 将返回 true。Find 方法为数组的每个元素调用此委托,在符合测试条件的第一个点处停止。

2.委托的清空

(1).在类中申明清空委托方法,依次循环去除委托引用。

方法如下: 

1     public MethodDelegate OnDelegate;                
2     public void ClearDelegate()        
3     {             
4         while (this.OnDelegate != null) 
5         {                 
6             this.OnDelegate -= this.OnDelegate;  
7         }        
8     } 
9  

(2).如果在类中没有申明清空委托的方法,我们可以利用 GetInvocationList 查询出委托引用,然后进行去除。

方法如下: 

 1  
 2     public MethodDelegate OnDelegate; 
 3     static void Main(string[] args)
 4     {
 5         Program test = new Program();
 6 
 7         if (test.OnDelegate != null) 
 8         { 
 9             System.Delegate[] dels = test.OnDelegate.GetInvocationList(); 
10             for (int i = 0; i < dels.Length; i++) 
11             {
12                 test.OnDelegate -= dels[i] as MethodDelegate;
13             }
14         }
15     }
16  

3.委托的特点

委托类似于 C++ 函数指针,但它们是类型安全的。
委托允许将方法作为参数进行传递。
委托可用于定义回调方法。
委托可以链接在一起;例如,可以对一个事件调用多个方法。
方法不必与委托签名完全匹配。

4.总结:

Action<T> 和 Func<T,TResult> 的功能是一样的,只是 Action<T> 没有返类型,

Delegate 至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型

Func 可以接受0个至16个传入参数,必须具有返回值

Action 可以接受0个至16个传入参数,无返回值

Predicate 只能接受一个传入参数,返回值为bool类型

原文地址:https://www.cnblogs.com/endv/p/6713385.html