星空网 > 软件开发 > ASP.net

C#基础知识之委托(delegate、Action、Func、predicate)

1. 什么是委托

官方解释

      委托是定义方法签名的类型,当实例化委托时,您可以将其实例化与任何具有兼容签名的方法想关联,可以通过委托实例调用方法。

个人理解

      委托通俗一点说就是把一件事情交给别人来帮助完成,而在C#中也可这样理解为委托就是动态调用方法。委托就是一个类,它定义了一个类,使得可以将方法当做另一个方法参数传递,这种将方法动态的赋给参数的做法,可以避免我们在程序中使用大量的 If-Else语句,同时使得程序具有更好的可拓展性。

2. delegate

Delegate至少0个参数,至多32个参数,可以是无返回值,也可以指定有返回值。

委托(delegate)语法定义如下:

public delegate int CalculatorAdd(int x,int y);

定义一个委托基本上就是定义了一个新类,所以可以在类的任何地方定义委托,即可以在类的内部定义,也可以在任何类的外部定义,还可以在命名空间中把委托定义为最顶层对象。在委托定义上添加一般的访问修饰符:public、private、protected等。

完成一个委托的四步骤:

  1.   声明一个委托。
  2.   声明一个方法对应委托。
  3.   方法来实例化委托。
  4.   调用委托。

代码如下:

class Program  {    // 1. 定义一个委托    public delegate int CalculatorAdd(int x, int y);    // 2. 声明一个方法对应委托    public static int Add(int x, int y)    {      return x + y;    }    static void Main()    {      // 3. 方法实例化委托      CalculatorAdd AddCacu = new CalculatorAdd(Add);      // 4. 调用委托      int s = Add(8, 9);      Console.Write(s);      Console.ReadLine();    }  }

3. Action

使用此委托以参数形式传递方法,不必显式定义一个封装无参数过程的委托。

Action是无返回值的泛型委托。

Action<int,string> 表示传入参数int,string类型,无返回值的委托。

Action<int,string,bool> 表示传入参数int,string,bool类型,无返回值的委托。

Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托。

Action至少0个参数,至多16个参数,无返回值。

代码如下:

    static void Main()    {      Action<string> action = new Action<string>(WriterMessage);      action("你好,我是Action委托");      Console.ReadLine();    }    private static void WriterMessage(string message)    {      Console.WriteLine(message + "\n");    }  

匿名方法:

    static void Main()    {      Action<string> say = delegate(string message)      {        Console.Write(message);      };      say("Hello Word");      Console.ReadLine();    }

 Lambda表达式:

    static void Main()    {      Action<string> say = message => Console.WriteLine(message);      say("Hello Word");      Console.ReadLine();    }    private static void WriterMessage(string message)    {      Console.WriteLine(message + "\n");    }

4. Func

Func是有返回值的泛型委托。

Func<int> 表示无参数,返回为int类型的委托。

Fun<object,string,int> 表示传入object、string参数,返回int类型的委托。

Func<T1,T2,,T3,int> 表示传入T1、T2、T3参数,返回int类型的委托。

Func至少O个参数,最多16个参数,根据返回值泛型返回。必须要有返回值,不能void。

匿名方法:

    static void Main()    {      Func<string, string> say = delegate(string message)      {        return message;      };      string saymessage = say("HelloWord");      Console.WriteLine(saymessage);      Console.ReadLine();    }    private static string WriterMessage(string message)    {      return message;    }

 Lambda表达式

    static void Main()    {      Func<string, string> say = (massage) => massage;      Console.WriteLine(say("HelloWord"));      Console.ReadLine();    }

5. predicate

表示定义一组条件并确定指定对象是否符合这些条件的方法。predicate主要用于Array和List的几种方法使用,用于搜索集合中的元素。

匿名方法:

   static void Main()    {      List<int> list = new List<int> { 6, 90, 24, 17, 29, 27, 64, 33, 72 };      Predicate<int> pre = delegate(int num)      {        return num > 30 ? true : false;      };      List<int> all = list.FindAll(pre);      foreach (int i in all)      {        Console.WriteLine(i);      }      Console.ReadLine();    }

Lambda表达式:

    static void Main()    {      List<int> list = new List<int> { 6, 90, 24, 17, 29, 27, 64, 33, 72 };      Predicate<int> pre = x => x > 30 ? true : false;      List<int> all = list.FindAll(pre);      foreach (int i in all)      {        Console.WriteLine(i);      }      Console.ReadLine();    }




原标题:C#基础知识之委托(delegate、Action、Func、predicate)

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

美国一件代发海外仓:https://www.goluckyvip.com/tag/90357.html
海外仓 英国:https://www.goluckyvip.com/tag/90359.html
滞销解决方法:https://www.goluckyvip.com/tag/9036.html
海外仓 美国:https://www.goluckyvip.com/tag/90366.html
虚拟海外仓 美国:https://www.goluckyvip.com/tag/90367.html
迪信通:https://www.goluckyvip.com/tag/9037.html
新质生产力加速数字贸易发展 卓尔智联集团实现营收利润双增长 :https://www.kjdsnews.com/a/1836409.html
独家丨B站广告位可跳转美团APP B站为电商平台引流再升级 :https://www.kjdsnews.com/a/1836410.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流