- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _03多播委托
- {
- public delegate void MyDelegate();
- class Program
- {
- static void Main(string[] args)
- {
- #region 多播委托
- ////一般第一个要使用=赋值,后续的方法可以使用+=来赋值。
- //Action<string> action = M1;
- //action += M2;
- //action += M3;
- //action += M4;
- //action += M5;
- //action -= M3;
- //action("王朋");
- //Console.ReadKey();
- #endregion
- MyDelegate md = new MyDelegate(T1);
- md = (MyDelegate)Delegate.Combine(md, new MyDelegate(T2), new MyDelegate(T3));
- // md();
- Delegate[] delegates = md.GetInvocationList();
- for (int i = 0; i < delegates.Length; i++)
- {
- (delegates[i] as MyDelegate)();
- }
- Console.ReadKey();
- //Queue<MyDelegate> queue = new Queue<MyDelegate>();
- //queue.Enqueue(new MyDelegate(T1));
- //queue.Enqueue(T2);
- //queue.Enqueue(T3);
- //queue.Dequeue();
- //queue.Dequeue();
- //queue.Dequeue();
- }
- static void T1()
- {
- Console.WriteLine("ok");
- }
- static void T2()
- {
- Console.WriteLine("ook");
- }
- static void T3()
- {
- Console.WriteLine("oook");
- }
- static void M1(string msg)
- {
- Console.WriteLine(msg);
- }
- static void M2(string msg)
- {
- Console.WriteLine(msg);
- }
- static void M3(string msg)
- {
- Console.WriteLine(msg);
- Console.WriteLine("第3个方法。。");
- }
- static void M4(string msg)
- {
- Console.WriteLine(msg);
- }
- static void M5(string msg)
- {
- Console.WriteLine(msg);
- }
- }
- }