委托进阶用法

本文详细介绍了C#中的Func和Action委托,展示了如何使用Lambda表达式实例化委托,以及在实际场景中如何运用它们进行函数传递和操作。涵盖了委托的使用、Lambda表达式的不同形式,以及Func和Action的区别和应用场景。
摘要由CSDN通过智能技术生成

Func, Action, Predict

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Event
{
    class Program
    {
        static void Main(string[] args)
        {
            委托:
            //MyDelegate<int> addDelegate = null;
            //addDelegate += Add;

            //Console.WriteLine(addDelegate(1, 2));
            //Console.ReadKey();

            //Lambda表达式:
            //第一种
            //MyDelegate<int> myDelegate = null;
            //myDelegate += (int a, int b) => a + b;
            //第二种
            //MyDelegate<int> myDelegate = (int a, int b) => a + b;
            //第三种
            //MyDelegate<int> myDelegate = (int a, int b) => { return a + b; };
            //Console.WriteLine(myDelegate(1, 2));

            //Func委托

            //Func<int, int, double> func = Add;
            //double result = func(10, 20);
            //Console.WriteLine(result);
            //Func<int, int, double> func = (a, b) => a + b;

            //Console.WriteLine(func(10, 20));
            //Console.ReadKey();

            //Func委托进阶

            //int[] nums = { 10, 9, 8, 7, 6, 5, 4, 3, 2 };

            //Console.WriteLine(CommonMethod(Add,nums,0,3)); // 0, 3是数组索引 :34
            //Console.WriteLine(CommonMethod(Mutiply, nums, 0, 5)); //151200
            //Console.ReadKey();


            //Func 委托必须要求所接收的方法有一个返回值. 如果没有返回值,可以使用Action委托
            //Action委托接收一个没有返回值的方法

            //Action委托
            //Action<string> act = (a) => Console.WriteLine("{0}欢迎你",a);
            //act("c");
            //Console.ReadKey();

            //predict委托
            /*
             * Predicate<T>委托定义如下:
             * public delegate bool Predicate<T>(T obj);
             * 解释:此委托返回一个bool值的方法
             * 在实际开发中,Predicate<T>委托变量引用一个“判断条件函数”,
             * 在判断条件函数内部书写代码表明函数参数所引用的对象应该满足的条件,条件满足时返回true
             */

            List<Student> stuList = new List<Student>()
            {
                   new Student(){ StudentId=1001,StudentName="小张"},
                   new Student(){ StudentId=1008,StudentName="小李"},
                   new Student(){ StudentId=1009,StudentName="小王"},
                   new Student(){ StudentId=1003,StudentName="小赵"},
                   new Student(){ StudentId=1005,StudentName="小刘"}
            };

            //查询学号大于1003的学员
            //List<T>集合中定义了一个FindAll方法:public T FindAll(Predicate<T> match)

            List<Student> list = stuList.FindAll(a => a.StudentId >= 1005);//传入的参数a是Student类型
            foreach (Student item in list)
            {
                Console.WriteLine(item.StudentName);
            }
            Console.ReadKey();
        }

        static int Add(int a, int b)
        {
            return a + b;
        }

        static int Mutiply(int i, int j)
        {
            return i * j;
        }
        static double Sub(double a,double b)
        {
            return a - b;
        }

        //使用func委托,将"运算本身"作为方法参数
        static int CommonMethod(Func<int, int, int> opration, int[] nums, int a, int b)
        {
            int result = nums[a];//把第一个值,作为基数(a+1:表示下一个值)
            for (int i = a+1; i <= b; i++)
            {
                result = opration(result,nums[i]);
            }
            return result;
        }
    }

    public delegate T MyDelegate<T>(T obj1, T obj2);
}

//【1】Func系列委托:多个重载版本
//public delegate TResult Func<TResult>();
//public delegate TResult Func<T,TResult>(T arg);
//public delegate TResult Func<T1,T2,TResult>(T1 arg1,T2 arg2);
//public delegate TResult Func<T1,T2,T3,TResult>(T1 arg1,T2 arg2,T3 arg3);
//public delegate TResult Func<T1,T2,T3,T4,TResult>(T1 arg1,T2 arg2,T3 arg3,T4 args);


/*
 * Action委托与Func系列类似,有若干个重载方法...,可以接收0-4个参数,且返回值为void类型的方法
 * public delegate void Action();
 * public delegate void Action<T>(T obj);
 * public delegate void Action<T1,T2>(T1 arg1,T2 arg2);
 * public delegate void Action<T1,T2,T3>(T1 arg1,T2 arg2,T3 arg3);
 * public delegate void Action<T1,T2,T3,T4>(T1 arg1,T2 arg2,T3 arg3,T4 arg4);
 * 
 *
*/

/*
 *【注意问题】
 * Func委托声明的最后一个泛型类型参数是委托所接收方法的《返回值类型》
 * 如果前面有泛型类型的参数,这个参数就是委托方法的形参类型
 * 
 *【牢记内容】
 * Func委托系列引用一个《有返回值的方法》,也就是将方法作为另一个方法的“参数”
 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

潘诺西亚的火山

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

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

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

打赏作者

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

抵扣说明:

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

余额充值