C# 委托、事件

buzheng11 / 2023-08-28 / 原文

一、委托、事件 定义
委托是将一种方法作为参数代入到另一种方法。事件是一种特殊的委托。
该事件在生成的时候会调用委托。
委托类似一个类,一种方法类型
返回值和参数跟的“委托”是一致的
事件为了实现委托
 
二、简单的实现
现在无需自己写委托类型!
自带 Action(无返回值)   Fun(带返回值)
class Program
    {
          delegate void D1();
          delegate int D2(int a, int b);
        static void Main(string[] args)
        {
            D1 d = F1;
            d();  //我是F1
            d = F2;
            d(); //我是F2

            D2 d2 = ADD;
            Console.WriteLine(d2(3, 5));//8

            Action a = F1;
            a();

            //int  sting 参数 无返回值
            Action<int,string> f8 = F8;
            f8(1,"22");

            // int int 参数  int 返回值
            Func<int, int, int> f = ADD;
            Console.WriteLine(f(6, 8));

            // int int 参数  string 返回值
            Func<int, int, string> f33 = F33;
            Console.WriteLine(f33(6, 8));
        }

        static void F1()
        {
            Console.WriteLine("我是F1");
        }

        static void F2()
        {
            Console.WriteLine("我是F2");
        }

        static void F8(int a,string b)
        {
            Console.WriteLine("我是F8");
        }

        static int ADD(int a,int b)
        {
            return a + b;
        }

        static string F33(int i,int j)
        {
            return "f33";
        }
    }
三、匿名委托   lambda  省略书写!
           //匿名委托方法
            Action f1 = delegate ()
            {
                Console.WriteLine("我是aaa");
            };
            f1();

            Action<string,int> f2 = delegate (string n, int i)
              {
                  Console.WriteLine($"n={n},i={i}");
              };
            f2("xw", 18);

            //没有返回值  方法体一行代码 省略{} 
            Action<int,int> f22=(a,b)=>Console.WriteLine(a+b);
            f22(1,2);

            //如何只有一个参数  参数()也可以省略
            Action<int> f6 = a => Console.WriteLine(a);
            f6(2);

            //如何没有参数 
            Action f7 = () => Console.WriteLine("frist");
            f7();

            Func<int, int, int> f3 = delegate (int i, int j)
               {
                   return i + j;
               };
            Console.WriteLine(f3(3,5));

            //lambda 表达式  省略delegate和参数
            Func<int, int, int> f4 = (i, j) =>
              {
                  return i + j;
              };

            //方法体一行代码  return 和 {} 都可以去掉
            Func<int, int, int> f5 = (a, b) => a + b;

            //f92为f91 省略
            Func<int, bool> f91 = (i) =>
             {
                 return i > 0;
             };
            Func<int, bool> f92 = i => i > 0;
            
            Console.WriteLine(f5(5, 5));
        //声明委托 CatShoutEventHandler
        public delegate void CatShoutEventHandler();
        //声明事件CatShout 事件类型是委托CatShoutEventHandler
        public event CatShoutEventHandler CatShout;
        public void newShout()
        {
            Console.WriteLine("瞄,我是{0}", name);
            if (CatShout!=null)
            {
                CatShout();
            }
        }
      //main 
            Cat cat1 = new Cat("Tom");
            Mouse mouse1 = new Mouse("Jerry");
            Mouse mouse2 = new Mouse("Jack");

            cat1.CatShout += new Cat.CatShoutEventHandler(mouse1.Run);//CatShout+ 不为空 执行这个函数
            cat1.CatShout += new Cat.CatShoutEventHandler(mouse2.Run);

            cat1.newShout();