C#委托


    class Program
    {
        #region 委托作用 解耦,减少重复代码、异步多线程

        #endregion

        #region Delegate委托 有返回值,无返回值都行 ,有参数无参数都行
        //public delegate void myDelegate();
        //static void Main(string[] args)
        //{
        //    myDelegate d = new myDelegate(method1);
        //    print(d);
        //}
        //public static void print(myDelegate d)
        //{
        //    d();
        //}
        //public static void method1()
        //{
        //    Console.WriteLine("Hello World");
        //    Console.ReadKey();
        //}
        #endregion

        #region Action委托 无返回值 有无参数都行
        //static void Main(string[] args)
        //{
        //    Action a = printInt;  //泛型代表方法有参数无返回值  没有泛型方法无参数无返回值  
        //    a(3);
        //    Action b = printInt;
        //    b();
        //}
        //public static void printInt()
        //{
        //    Console.WriteLine("Hello World!");
        //    Console.ReadKey();
        //}
        //public static void printInt(int a)
        //{
        //    Console.WriteLine(a);
        //    Console.ReadKey();
        //}
        #endregion

        #region Func委托 有返回值  有无参数都行

        //static void Main(string[] args)
        //{
        //    Func a = printInt;  //Func 有返回值,最后一个泛型是返回值类型
        //    Console.WriteLine(a(3));
        //    Console.ReadKey();
        //}
        //public static string printInt(int a)
        //{
        //    return "1" + a;
        //}
        #endregion

        #region 多播委托 一般是无返回值的 有无参数都行
        //static void Main(string[] args)
        //{
            //Action a = test1;
            //a += test2;
            //a -= test2;
            //a(2);

            //lambda表达式注册之后用多播委托去不掉
            //Action a = (b) => { Console.WriteLine("test1" + b); };
            //a += (b) => { Console.WriteLine("test2" + b); };
            //a -= (b) => { Console.WriteLine("test2" + b); };
            //a(3);

            //Action a = show;
            //a += new Person().study;
            //a -= new Person().study;//两个方法不是同一个对象调用的====>去不掉==》调用两次
            //a();

            #region 方式二
            //Delegate[] list = a.GetInvocationList();
            //foreach (Delegate d in list)
            //{
            //    d.DynamicInvoke(0);
            //}
            #endregion

            //Console.ReadKey();
        //}
        static void test1(int a)
        {
            Console.WriteLine("test1" + a);
        }
        static void test2(int b)
        {
            Console.WriteLine("test2" + b);
        }
        static void show()
        {
            Console.WriteLine("show");
        }
        #endregion

        #region 匿名方法
        public delegate void ElapsedEventHandler(int sender, int e);
        static void Main(string[] args)
        {
            #region 正常方法
            //Func func = test;
            //Console.WriteLine(func(2, 2));
            //Console.ReadKey();
            #endregion

            #region 匿名方法 有无参数,有无返回值都行(当参数没用到的时候参数可以省略)                 
            //Func func = delegate(int a,int b)
            //  {
            //      return 1 + 2;
            //  };
            //Console.WriteLine(func(2, 3));

            //Console.ReadKey();
            #endregion

            #region 匿名方法 (当参数没用到的时候参数可以省略)                 
            //Func func = delegate
            //{
            //    return 1 + 2;
            //};
            //Console.WriteLine(func(2, 3));
            //Console.ReadKey();
            #endregion

            #region 匿名方法 无参数()可以省略
            //Action func = delegate
            //{
            //    Console.WriteLine("234");
            //};
            //func();
            //Console.ReadKey();
            #endregion

            #region lambda表达式
            //Func func = (arg1, arg2) =>
            // {
            //     return arg2 + arg1;
            // };
            //Console.WriteLine(func(3, 2));
            //Console.ReadKey();
            #endregion
        }
        static int test(int arg1, int arg2)
        {
            return arg1 + arg2 + 3;
        }
        static void test2(int arg1, int arg2)
        {
            Console.WriteLine(arg1 + arg2);
            Console.ReadKey();
        }
        #endregion
    }
    public class Person
    {
        public void study()
        {
            Console.WriteLine("study!!!!!!!!");
        }
        public void Method()
        {
            Console.WriteLine("Method!!!!!!!!");
        }
    }

 更多详情:https://www.cnblogs.com/jixiaosa/p/10687068.html