首页 > 代码库 > C# 委托, 铭轩同学原创

C# 委托, 铭轩同学原创

委托的定义:

    (1) 将方法作为变量使用的一种机制,就是将方法当作变量用(声明,赋值,传参)

      (2) 将变量当作方法来用,首先就要去声明变量,就要考虑变量的类型,就是(委托变量,对应方法的返回值,参数等),顾名思义:委托就是委托别人去干某些事情,下面是一个实例方法的实现 例如:我现在饿了,但是我很懒,就是不想出去买,所以这时候我可以委托室友帮我带一份,这样我就已经实现委托了

    

 class Program
    {
        //声明一个委托
        delegate int DelegateCompare(object o1, object o2);

        delegate int DelegateTotal(int num1, int num2);

        static void Main(string[] args)
        {

            //比较两个整数
            DelegateCompare del = intCompare;  //简写
            Console.WriteLine(del(1, 2));

            //比较两个字符的长度
            del = new DelegateCompare(strLengthCompare); //完整写法
            Console.WriteLine(del("123456", "admin"));

            del = PersonAgeCompare;  //简写
            Console.WriteLine(del(new Person("张三", 18), new Person("李四", 20)));

            /*-------------取最大值-----------*/
            DelegateCompare delMax;

            delMax = intCompare; //整数
            Console.WriteLine(GetMax(new object[] { 1, 5, 3, 6, 8, 7 }, delMax));

            delMax = strLengthCompare; //字符串长度
            Console.WriteLine(GetMax(new object[] { "aa", "a", "aaa", "aaaa", "aaaaaaaa", "a" }, delMax));

            delMax = PersonAgeCompare;  //Person类的Age属性
            Console.WriteLine(((Person)GetMax(new object[] { new Person("张三", 18), new Person("李四", 14), new Person("万物", 20) }, delMax)).Name);

            /*----------匿名委托----------*/

            //标准写法
            DelegateCompare del2 = delegate(object o1, object o2) { return (int)o1 - (int)o2; };
            Console.WriteLine("匿名委托" + del2(5, 3));
            //lambda 表达式
            DelegateCompare del3 = (object o1, object o2) => (int)o1 - (int)o2;   //del3 同等于Del4
            DelegateCompare del4 = (object o1, object o2) => { return (int)o1 - (int)o2; };

            /*--------系统委托-----------*/

            //lambda 表达式
            string[] names = { "agen", "balen", "coure", "apple" };
            string[] findNameA = Array.FindAll<string>(names, delegate(string v) { return v.StartsWith("a"); });
            string[] findNameB = Array.FindAll<string>(names, v => v.StartsWith("a"));
            Console.WriteLine(string.Join("-", findNameA));
            Console.WriteLine(string.Join("-", findNameB));


            /*----------------多播委托----------*/
            //多播委托一般情况下 要求没有返回值, 若有, 则返回最后一个方法的返回值
            DelegateTotal delTotal = He;
            delTotal += Cha;
            delTotal += Ji;
            Console.WriteLine(delTotal(2, 4));
            Console.ReadKey();
        }


        /// <summary>
        /// 获取array最大值
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="del"></param>
        /// <returns></returns>
        static object GetMax(object[] arr, DelegateCompare del)
        {
            if (arr.Length > 1)
            {
                object _max = arr[0];
                for (int i = 1; i < arr.Length; i++)
                {
                    if (del(_max, arr[i]) < 0)
                    {
                        _max = arr[i];
                    }
                }
                return _max;
            }
            return null;
        }


        /// <summary>
        /// 比较两个Person类的Age属性 大于0  o1大  等于0 相等   小于0  o2大
        /// </summary>
        /// <param name="o1"></param>
        /// <param name="o2"></param>
        /// <returns></returns>
        static int PersonAgeCompare(object o1, object o2)
        {
            return ((Person)o1).Age - ((Person)o2).Age;
        }

        /// <summary>
        /// 比较两个字符串的长度 大于0  o1大  等于0 相等   小于0  o2大
        /// </summary>
        /// <param name="o1"></param>
        /// <param name="o2"></param>
        /// <returns></returns>
        static int strLengthCompare(object o1, object o2)
        {
            return o1.ToString().Length - o2.ToString().Length;
        }

        /// <summary>
        /// 比较两个数的大小 大于0  o1大  等于0 相等   小于0  o2大
        /// </summary>
        /// <param name="o1"></param>
        /// <param name="o2"></param>
        /// <returns></returns>
        static int intCompare(object o1, object o2)
        {
            return (int)o1 - (int)o2;
        }

        /// <summary>
        /// 求和
        /// </summary>
        /// <param name="num1"></param>
        /// <param name="num2"></param>
        /// <returns></returns>
        static int He(int num1, int num2)
        {
            Console.WriteLine("----求和----");
            return num1 + num2;
        }
        /// <summary>
        /// 求差
        /// </summary>
        /// <param name="num1"></param>
        /// <param name="num2"></param>
        /// <returns></returns>
        static int Cha(int num1, int num2)
        {
            Console.WriteLine("----求差----");
            return num1 - num2;
        }
        /// <summary>
        /// 求积
        /// </summary>
        /// <param name="num1"></param>
        /// <param name="num2"></param>
        /// <returns></returns>
        static int Ji(int num1, int num2)
        {
            Console.WriteLine("----求积----");
            return num1 * num2;
        }
    }

    //定义一个Person类
    class Person
    {
        //姓名
        private string name = "";
        //年龄
        private int age = 0;
        //构造方法
        public Person(string PName, int PAge)
        {
            this.Name = PName;
            this.Age = PAge;
        }
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

    }