C#中的三种内置委托----7

140 阅读3分钟

委托和事件

委托

委托是一种容器,容器里面放的是函数方法。而函数的形式各不相同,体现在参数、返回值的不同。所以做委托之前,需要定义好委托容器存放函数的类型,即委托类型。定义好委托类型后,将函数加入委托容器后。只要触发委托调用,委托就会将容器中的每个函数都调用一次。

事件

事件在类中声明并生成,且通过使用同一个类或其它类中的委托和事件处理程序关联。包含事件的类用于发布事件,又称发布器类。其它接收该事件的类被称为订阅器类。事件使用发布---订阅模式。

Action委托

.Action说明

  • Action是.NET Framework内置的泛型委托,可以使用Action委托以参数形式传递方法,而不用显示声明自定义的委托。封装的方法必须与此委托定义的方法签名相对应。也就是说,封装的方法必须具有一个通过值传递给它的参数,并且不能有返回值。
  • Action的特点:参数0-16个,且没有返回值。

Func委托

  • Func委托的特点:参数0-16个,必须有返回值

Func委托实战:

实例类

 internal class Student
    {
        private string name;
        private int age;
        private int score;
        public Student(string name, int age, int score)
        {
            this.name = name;
            this.age = age;
            this.score = score;
        }
        //自我介绍
        public void SelfIntroduce()
        {
            Console.WriteLine("姓名:{0},年龄:{1},分数:{2}",this.name,this.age,this.score);
        }
        //年龄的比较
        public static int AgeCompare(Student firstStudent, Student secondStudent)
        { 
            return firstStudent.age - secondStudent.age;
        }
        //分数的比较
        public static int ScoreCompare(Student firstStudent, Student secondStudent)
        { 
            return  firstStudent.score - secondStudent.score;
        }
    }

测试类

 internal class Program
    {
        static void Main(string[] args)
        {
            StudentSort();
            Console.Read();
        }

        //冒泡排序的变形
        public static void BuBBleSort<T>(T[] array,Func<T,T,int> Compare)
        { 
            for (int i = 0; i < array.Length-1; i++) 
            {
                for (int j = 0; j < array.Length-1-i; j++)
                {
                    if (Compare(array[j], array[j+1])>0)
                    {
                        T temp;
                        temp = array[j];
                        array[j] = array[j + 1];
                        array[j+1]= temp;
                    }
                }
            }
        }

        //方法,学生排序
        public static void StudentSort()
        {
            Student student1 = new Student("赵", 18, 90);
            Student student2 = new Student("钱", 15, 92);
            Student student3 = new Student("孙", 18, 97);
            Student student4 = new Student("李", 15, 35);
            Student student5 = new Student("周", 18, 40);
            Student student6= new Student("吴", 15, 55);
            Student student7 = new Student("郑", 18, 10);
            Student student8 = new Student("王", 15, 255);
            Student[] students= { student1, student2, student3, student4, student5, student6, student7, student8 };
            Func<Student, Student, int> funcAge = Student.ScoreCompare;
            BuBBleSort(students, funcAge);

            foreach (Student student in students) 
            { 
                student.SelfIntroduce();
            }
           
        }
       
    }

Predicate委托

特点:只允许有一个输入对象,并且会返回bool值。 介绍:表示定义一组条件并确定指定对象是否符合这些条件的方法。 使用场景:检索、判定若干对象中符合条件方法的对象。

实例类

 internal class NumJudge
    {
        //判断目标整数是否为质数
        public static bool PrimeJudge(int num)
        { 
            for (int i = 2; i < num-1; i++)
            {
                if (num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }

        public static bool EvenJudge(int num)
        {
            if (num % 2 == 0)
            { 
                return true;
            }
            return false;
        }
    }

测试类1,测试数字是否是质数

internal class NumJudge
    {
        //判断目标整数是否为质数
        public static bool PrimeJudge(int num)
        { 
            for (int i = 2; i < num-1; i++)
            {
                if (num % i == 0)
                {
                    return false;
                }
            }
            return true;
        }
 static void ShowAllNumber(int startNum, int endNum, Predicate<int> predicate)
        { 
            for (int i = startNum; i <= endNum; i++) 
            {
                if (predicate(i))
                {
                    Console.WriteLine(i);
                }
            }
        }
      static void Main(string[] args)
        {
           // StudentSort();
           Predicate<int> pred=NumJudge.PrimeJudge;
            ShowAllNumber(1, 100, pred);
            Console.Read();
        }

测试类2,测试学生数组中的年龄是否等于15,并将其挑选出来

 static void StudentPredicate()
        {
            Student student1 = new Student("赵", 18, 90);
            Student student2 = new Student("钱", 15, 92);
            Student student3 = new Student("孙", 18, 97);
            Student student4 = new Student("李", 15, 35);
            Student student5 = new Student("周", 18, 40);
            Student student6 = new Student("吴", 15, 55);
            Student student7 = new Student("郑", 18, 10);
            Student student8 = new Student("王", 15, 255);
            Student[] students = { student1, student2, student3, student4, student5, student6, student7, student8 };

            //创建委托对象
            Predicate<Student> predicate = stu => (stu.age == 15);
            //检索学生中满足上述条件的学生

            Student[] students1=Array.FindAll<Student>(students, predicate);
            foreach (Student student in students1)
            {
                student.SelfIntroduce();
            }