zoukankan      html  css  js  c++  java
  • C#委托Action、Action<T>、Func<T>、Predicate<T>

    CLR环境中给我们内置了几个常用委托Action、 Action<T>、Func<T>、Predicate<T>,一般我们要用到委托的时候,尽量不要自己再定义一 个委托了,就用系统内置的这几个已经能够满足大部分的需求,且让代码符合规范。

    一、Action

    Action封装的方法没有参数也没有返回值,声明原型为:

    1 public delegate void Action();

    用法如下:

    复制代码
    1  public void Alert()
    2  {
    3     Console.WriteLine("这是一个警告");
    4  }
    5  
    6  Action t = new Action(Alert); //  实例化一个Action委托 
    7  t();
    复制代码

    如果委托的方法里的语句比较简短,也可以用Lambd表达式直接把方法定义在委托中,如下:

    1 Action t = () => { Console.WriteLine("这是一个警告"); };
    2 t();

    二、Action<T>

    Action<T>是Action的泛型实现,也是没有返回值,但可以传入最多16个参数,两个参数的声明原型为:

    1 public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2);

    用法如下:

    复制代码
    1 private void ShowResult(int a, int b)
    2 {
    3     Console.WriteLine(a + b);
    4 }
    5 
    6 Action<int, int> t = new Action<int, int>(ShowResult);//两个参数但没返回值的委托
    7 t(2, 3);
    复制代码

    同样也可以直接用Lambd表达式直接把方法定义在委托中,代码如下: 

    1 Action<int, int> t = (a,b) => { Console.WriteLine(a + b); };
    2 t(2, 3);

    三、Func<T>

    Func<T>委托始终都会有返回值,返回值的类型是参数中最后一个,可以传入一个参数,也可以最多传入16个参数,但可以传入最多16个参数,两个参数一个返回值的声明原型为:

    1 public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);

    用法如下:

    复制代码
    1 public bool Compare(int a, int b)
    2 {
    3     return a > b;
    4 }
    5 
    6 Func<int, int, bool> t = new Func<int, int, bool>(Compare);//传入两个int参数,返回bool值
    7 bool result = t(2, 3);
    复制代码

    同样也可以直接用Lambd表达式直接把方法定义在委托中,代码如下:

    1 Func<int, int, bool> t = (a, b) => { return a > b; };
    2 bool result = t(2, 3);

    四 、Predicate<T>

    Predicate<T>委托表示定义一组条件并确定指定对象是否符合这些条件的方法,返回值始终为bool类型,声明原型为:

    1 public delegate bool Predicate<in T>(T obj);

     用法如下:

    复制代码
    1 public bool Match(int val)
    2 {
    3     return val > 60;
    4 }
    5 
    6 Predicate<int> t = new Predicate<int>(Match);   //定义一个比较委托
    7 int[] arr = { 13, 45, 26, 98, 3, 56, 72, 24 };            
    8 int first = Array.Find(arr, t);                 //找到数组中大于60的第一个元素
    复制代码

    同样也可以直接用Lambd表达式直接把方法定义在委托中,代码如下:

    1 Predicate<int> t = val => { return val > 60;};   //定义一个比较委托
    2 int[] arr = { 13, 45, 26, 98, 3, 56, 72, 24 };            
    3 int first = Array.Find(arr, t);                  //找到数组中大于60的第一个元素

    总结:

    • 如果要委托的方法没有参数也没有返回值就想到Action
    • 有参数但没有返回值就想到Action<T>
    • 无参数有返回值、有参数且有返回值就想到Func<T>
    • 有bool类型的返回值,多用在比较器的方法,要委托这个方法就想到用Predicate<T>

     转载地址:http://www.cnblogs.com/maitian-lf/p/3671782.html

  • 相关阅读:
    关于 Kubernetes 中的 Volume 与 GlusterFS 分布式存储
    使用 Kubeadm 升级 Kubernetes 版本
    Kubernetes 中的核心组件与基本对象概述
    使用 Kubeadm 安装部署 Kubernetes 1.12.1 集群
    比较 Spring AOP 与 AspectJ
    关于 Spring Security OAuth2 中 CORS 跨域问题
    Prometheus 入门与实践
    MySQL 分支的选择:Percona 还是 MariaDB
    Spring Boot 集成 Swagger2 与配置 OAuth2.0 授权
    关于 Spring Security 5 默认使用 Password Hash 算法
  • 原文地址:https://www.cnblogs.com/louby/p/7803143.html
Copyright © 2011-2022 走看看