• 什么是委托,如何实现委托


    Step 1.为什么用委托

    其实很简单,当我们需要把方法传递给方法时,在C#中我们就需要使用委托。

    委托类似于 C++ 函数指针,但它是托管的,类型安全的。

     

    Step 2.怎么声明委托

    委托的语法很简单,这点类似方法的定义,只是在前面多了个关键字:Delegate

    我们声明一个委托,其实就是告诉编译器这种类型的委托代表哪种类型的方法,这样我们就可以创建一个或多个委托实例。

     

    Step 3.举例

    实例1

     

    using System;

    namespace Delegate
    {

        // 定义了一个返回值为void,两个int型参数的委托
        public delegate void CalculateInt(int a,int b);

        class Calculate
        {
            public static void Add(int a, int b)
            {
                Console.WriteLine(a + b);
            }

            public static void Square(int a, int b)
            {
                Console.WriteLine(a * b);
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                CalculateInt calculateInt = new CalculateInt(Calculate.Add);// 实例化委托

                // 这里实现了一个多播委托,特别强调的是多播委托的返回值类型必须为void的类型
                calculateInt += new CalculateInt(Calculate.Square);
                calculateInt(2, 4);
                Console.Read();
               
            }
        }
    }

     实例 2

     可以向排序算法传递对比较两个对象的方法的引用。分离比较代码使得可以采用更通用的方式编写算法。

    public delegate bool CompareObject(object obj1,object obj2);

        class SortObjectArray
        {

             // coampareObject是一个委托实例
            public static void Sort(object[] arrayList, CompareObject coampareObject)
            {
                for (int i = 0; i < arrayList.Length; i++)
                {
                    for (int j = i + 1; j < arrayList.Length; j++)
                    {
                        if(coampareObject(arrayList[i],arrayList[j]) )
                        {
                            object temp = arrayList[i];
                            arrayList[i] = arrayList[j];
                            arrayList[j] = temp;
                        }
                    }
                }
            }
        }

        class Student
        {
            string stuName;
            public Student(string stuName)
            {
                this.stuName= stuName;
            }

            public string Name
            {
                get { return stuName; }
            }

            public static bool CompareName(object obj1, object obj2)
            {
                Student stu1 = obj1 as Student;
                Student stu2 = obj2 as Student;
                if (null != stu1 && null != stu1)
                {
                    if (stu1.stuName.CompareTo(stu2.stuName) > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                Student[] students = { new Student("Matthew"), new Student("Norris") ,new Student("Grant"),new Student("Aron")};
                SortObjectArray.Sort(students,new CompareObject(Student.CompareName));
                foreach (Student stu in students)
                {
                    Console.WriteLine(stu.Name);
                }
                Console.Read();
               
            }
        }

  • 相关阅读:
    OKHttp的使用
    多点触控
    事件分发机制的理解
    Activity的四种启动模式
    反射动态建表
    px,dp,sp三者的转换
    动态广播和静态广播的区别
    字符串转日期格式
    hive Metastore contains multiple versions
    记一次 qW3xT.4,解决挖矿病毒。
  • 原文地址:https://www.cnblogs.com/matthew/p/1268024.html
Copyright © 2020-2023  润新知