委托

     委托其实是一个类  delegate

实现方法的叠加调用 

一.基本用法 

public delegate int Tpons(int x);
    class Program
    {
         
        static void Main(string[] args)
        {
            Tpons t = new Tpons(ss.max);
            t += ss.max1;
            t(4);
            Console.ReadKey();
        }
    }
    class ss
    {
        public static int max(int q)
        {
            Console.WriteLine(q*q);
            return q * q;
        }
        public static int max1(int q)
        {
            Console.WriteLine(q - q);
            return q -q;
        }
    }

 二.与泛型编程一起使用

 public delegate Q tops<Q>(Q b);//  声明了一个返回值与参数都为Q的 委托     委托+泛型编程
        tops<int> asr = new tops<int>(Until.func<int>);//asr里的 委托类型与方法类型都是int了 
        int[] arr = { 1213213, 324, 23, 5 };
        int Func(int a)
        {
            return a;
        }
        public static void Transform<T>(T[] valuse, tops<T> method)//method里面的返回值与参数都是 T类型的
        {
                                           
        }
        void us()
        {
            Transform<int>(arr, Func);
        }
        public  int   assas(int v)=>v* v;//想当于 return v*v; 语法糖

    }
    class Until
    {
        public static T func<T> (T a)//泛型编程 在用到 func时 必须指定 T的类型
        {
            Console.WriteLine(a);
            Console.ReadKey();
            return a;
        }
       

    }

  三 Func 与 Action

      

 

 Func<int, int> t = new Func<int, int>(dle);//有返回值有 参数就可用func
        Action<int> r = new Action<int>(funn);// 有参数无返回值 可以用 Action
        public  static int dle(int a)
        {
            return a * a;
        }
         void funn(int a,Func<int ,double> q)
        {
           
        }

  

posted @ 2021-04-11 15:48  pursuit_purity  阅读(72)  评论(0)    收藏  举报