委托学习续:Action、Func和Predicate

本文深入解析了C#中的委托概念,并详细介绍了Action、Func和Predicate的具体用法,通过实例展示了如何使用这些委托来简化代码并提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值