委托学习续:Action、Func和Predicate

我们先看一个上一章的委托的例子:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Test
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             new Program();
14 
15             Console.ReadKey();
16         }
17 
18         public delegate void AddDelegate(float a, int b);
19 
20         public Program()
21         {
22             AddDelegate add = addFunc;
23 
24             add(11.11f, 123);
25         }
26 
27         private void addFunc(float a, int b)
28         {
29             double c = a + b;
30             Console.WriteLine(c);
31         }
32     }
33 }

Action:

Action可以看做C#为我们提供的内置的没有返回值的委托,用在第一个例子里可以去掉委托的声明,代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Test
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             new Program();
14 
15             Console.ReadKey();
16         }
17 
18         public Program()
19         {
20             //指定好参数数量和类型即可立即使用, 无需显示的定义委托
21             Action<float, int> add = addFunc;
22 
23             add(11.11f, 123);
24         }
25 
26         private void addFunc(float a, int b)
27         {
28             double c = a + b;
29             Console.WriteLine(c);
30         }
31     }
32 }

同时,匿名函数和Lambda的写法也是允许的,详情可以查看上一篇笔记。

Func:

类似Action,Func是C#为我们提供的内置的具有返回值的委托,而返回值的类型为最后一个被指定的类型,请看下面的例子:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Test
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             new Program();
14 
15             Console.ReadKey();
16         }
17 
18         public Program()
19         {
20             //指定好参数数量和类型即可立即使用, 无需显示的定义委托
21             //对于 Func 委托来说, 最后指定的类型为返回的类型
22             Func<float, int, string> add = addFunc;
23 
24             string result = add(11.11f, 123);
25             Console.WriteLine(result);
26         }
27 
28         private string addFunc(float a, int b)
29         {
30             double c = a + b;
31             Console.WriteLine(c);
32             return "hello: " + c.ToString();
33         }
34     }
35 }

Predicate:

该委托专门用于过滤集合中的元素,下面我们看一个例子,保留数组中的正整数:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Test
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             new Program();
14 
15             Console.ReadKey();
16         }
17 
18         public Program()
19         {
20             //指定的类型为需要过滤的数组的元素类型
21             Predicate<int> filter;
22             filter = IntFilter;
23 
24             int[] nums = new int[8]{12, -33, -89, 21, -15, 29, 40, -52};
25             //开始进行过滤, 返回 true 表示留下当前元素
26             int[] result = Array.FindAll(nums, filter);
27 
28             for(int i = 0; i < result.Length; i++)
29             {
30                 Console.WriteLine(result[i]);
31             }
32         }
33 
34         private bool IntFilter(int i)
35         {
36             if (i >= 0)
37             {
38                 return true;
39             }
40             return false;
41         }
42     }
43 }
原文地址:https://www.cnblogs.com/hammerc/p/4422940.html