C#__多播委托的基本使用

// 多播委托:包含多个方法,一般声明返回值为void。

        private static void Test1()
        {
            Console.WriteLine("Test1");
        }
        private static void Test2()
        {
            Console.WriteLine("Test2");
        }

 // 调用

            // 多播委托
            Action action1 = Test1;
            action1 += Test2;
            action1(); // Test1 Test2

            action1 += Test2;
            action1 -= Test1;
            action1(); // Test2 Test2

            Delegate[] delegates = action1.GetInvocationList(); // 定义了一个委托数组
            foreach(Delegate d in delegates)
            {
                d.DynamicInvoke(); // 调用委托所表示的方法
            } // Test2 Test2

猜你喜欢

转载自blog.csdn.net/qq_57233919/article/details/132232981