C#委托:Predicate<>、 Func<> 、 Action<>的简单理解

Predicate

使用前

namespace Read
{
    public delegate void GetShowMember(int[] num);
    class Program
    {
        public static void Main(string[] args)
        {
            var arr = new int[] { 1, 2, 3, 4, 65, 4, 3, 2, 25, 15123, 53 };
            //使用正常的委托方法  委托是一种类型,需要创建实例
            var DelShow = new GetShowMember(ShowMember);
            DelShow(arr);
            Console.ReadKey();
        }

       public static bool ShowMember(int[] number)
        {
            bool ret = false;
            Console.WriteLine(string.Join(",", number));
            return ret;
        }
}


使用后:

    

           //Predict 委托表示的方法传入一个类型参数,返回值必须是bool类型;
            var DelPredictShow = new Predicate<int[]>(ShowMember );
            DelPredictShow(arr);


            //等价于
            Predicate<int[]> show = ShowMember;
            show(arr);


Func:

使用前


namespace Read
{
    public delegate int GetMax(int[] num);
    class Program
    {
        public static void Main(string[] args)
        {

            int[] in1 = { 10, 2, 73, 94, 65, 49, 399, 2, 25, 151, 23, 53 };
            var arr = new int[] { 1, 2, 3, 4, 65, 4, 3, 2, 25, 15123, 53 };
            //使用委托
            var DelGetMax = new GetMax(GetMaxNum);
            DelGetMax(arr);
            DelGetMax(in1);
            Console.ReadKey();
        }
        public static int GetMaxNum(int[] number)
        {
            int max = number[0];
            foreach (var item in number)
            {
                if (max < item)
                    max = item;
            }
            Console.WriteLine("Get the Max {0}", max);
            return max;
        }
   }
}

使用后

 

         //<int[],int>后面是返回值,前面是参数,参数可以有0~16个,返回值必须有一个
            var FuncGetMax = new Func<int[], int>(GetMaxNum );
            FuncGetMax(in1);
            FuncGetMax(arr);


            //等价于          
            Func<int[], int> func = GetMaxNum;
            func(in1);
            func(arr);




Action使用前

namespace Read
{
    public delegate void ShowMember(int[] num);
    class Program
    {
        public static void Main(string[] args)
        {

            int[] in1 = { 10, 2, 73, 94, 65, 49, 399, 2, 25, 151, 23, 53 };

            var DelShowMember = new ShowMember(ShowMemberOfNumber);
            DelShowMember(in1);

            Console.ReadKey();
        }

        public static void ShowMemberOfNumber(int[] number)
        {
            Console.WriteLine("Show the member of the array");
            Console.WriteLine(string.Join(",", number));
        }
    }
}

使用后:

            //<int[]>为参数,可以有0~16个,没有返回值
            var DelActionShow = new Action<int[]>(ShowMemberOfNumber );
            DelActionShow(in1);

	    //等价于
            Action<int[]> action = ShowMemberOfNumber;
            action(in1);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一枚努力的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值