这篇文章主要给大家介绍了关于C#中Action、Func和Predicate如何使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
前言
委托是一个类型安全的函数指针,它可以引用与委托具有相同签名的方法。委托常用于实现回调方法或者事件机制,在C#中一般用 "delegate" 关键字声明。你可以声明一个和类平级的委托,也可以嵌套在类中。
Func 和 Action 是什么,如何使用?
两者最基本的区别是,前者适合那些需要带返回值的委托,后者适合那些不带返回值的委托。
Func 所引用的方法接收一个或者多个入参并带有一个返回值,Action所引用的方法接收一个或者多个参数并且没有返回值,换句话说,你的委托所引用的方法没有返回值,这时候适合用 Action。
Predicate所引用的方法接收一个或者多个泛型参数并且返回一个 bool 值,你可以假定它等价于 Func<T,bool>,Predicate 常用于对 collection 进行一组条件检索。
C# 中使用 Action
你可以使用 委托 去实现事件和回调方法,C#委托非常类似于C++中的函数指针,但是 C# 中的 委托 是类型安全的,你可以将方法作为参数传递给委托从而让委托指向该方法。
下面的代码片段展示了 Action 委托的语法结构。
Action<TParameter>
接下来的代码清单展示了如何使用 Action 委托,当下面的代码执行结束后会在控制台打印 Hello !!!。
static void Main(string[] args)
{
Action<string> action = new Action<string>(Display);
action("Hello!!!");
Console.Read();
}
static void Display(string message)
{
Console.WriteLine(message);
}
C# 中使用 Func
现在我们一起学习下 Func 委托,下面是 Func 的语法结构。
Func<TParameter, TOutput>
接下来的代码片段展示了如何在 C# 中使用 Func 委托,最终方法会打印出 Hra(基本薪资的 40%) 的值,基本薪资是作为参数传下去的,如下代码所示:
static void Main(string[] args)
{
Func<int, double> func = new Func<int, double>(CalculateHra);
Console.WriteLine(func(50000));
Console.Read();
}
static double CalculateHra(int basic)
{
return (double)(basic * .4);
}
值得注意的是,Func 委托的第二个参数表示方法的返回值,在上面这个例子中,它就是计算后的 Hra 值,作为 double 型返回。
C# 中使用 Predicate
Predicate 委托常用于检索 collection,下面是 Predicate 的语法结构。
Predicate<T>
值得注意的是, Predicate<T> 差不多等价于 Func<T,bool>。
考虑下面的 Customer 实体类。
class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
接下来生成一个 customer 集合并且丢一些数据进去,如下代码:
List<Customer> custList = new List<Customer>();
custList.Add(new Customer { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", State = "Telengana", City = "Hyderabad", Address = "Begumpet", Country = "India" });
custList.Add(new Customer { Id = 2, FirstName = "Steve", LastName = "Jones", State = "OA", City = "New York", Address = "Lake Avenue", Country = "US" });
接下来是完整的代码片段展示了如何使用 Predicate 去检索集合。
static void Main(string[] args)
{
List<Customer> custList = new List<Customer>();
custList.Add(new Customer { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", State = "Telengana", City = "Hyderabad", Address = "Begumpet", Country = "India" });
custList.Add(new Customer { Id = 2, FirstName = "Steve", LastName = "Jones", State = "OA", City = "New York", Address = "Lake Avenue", Country = "US" });
Predicate<Customer> hydCustomers = x => x.Id == 1;
Customer customer = custList.Find(hydCustomers);
Console.WriteLine(customer.FirstName);
Console.Read();
}
当上面的代码被成功执行, 控制台将会输出 Joydip 。
译文链接:https://www.infoworld.com/article/3057152/how-to-work-with-action-func-and-predicate-delegates-in-csharp.html?nsdr=true
总结
到此这篇关于C#中Action、Func和Predicate如何使用的文章就介绍到这了,更多相关C#中Action、 Func和Predicate使用内容请搜索得得之家以前的文章希望大家以后多多支持得得之家!
本文标题为:C#中的Action、Func和Predicate如何使用
- C语言详解float类型在内存中的存储方式 2023-03-27
- C++ 数据结构超详细讲解顺序表 2023-03-25
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- C语言qsort()函数的使用方法详解 2023-04-26
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- ubuntu下C/C++获取剩余内存 2023-09-18
- Easyx实现扫雷游戏 2023-02-06
- Qt计时器使用方法详解 2023-05-30
