C#中的Action、Func和Predicate如何使用

2022-07-26,,,

前言

委托是一个类型安全的函数指针,它可以引用与委托具有相同签名的方法。委托常用于实现回调方法或者事件机制,在c#中一般用 "delegate" 关键字声明。你可以声明一个和类平级的委托,也可以嵌套在类中。

funcaction 是什么,如何使用

两者最基本的区别是,前者适合那些需要带返回值的委托,后者适合那些不带返回值的委托。

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如何使用.doc》

下载本文的Word格式文档,以方便收藏与打印。