C# 多播委托

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace _03多播委托  
  7. {  
  8.     public delegate void MyDelegate();  
  9.   
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             #region 多播委托  
  15.   
  16.             ////一般第一个要使用=赋值,后续的方法可以使用+=来赋值。  
  17.             //Action<string> action = M1;  
  18.             //action += M2;  
  19.             //action += M3;  
  20.             //action += M4;  
  21.             //action += M5;  
  22.   
  23.             //action -= M3;  
  24.   
  25.             //action("王朋");  
  26.             //Console.ReadKey();  
  27.  
  28.             #endregion  
  29.   
  30.             MyDelegate md = new MyDelegate(T1);  
  31.             md = (MyDelegate)Delegate.Combine(md, new MyDelegate(T2), new MyDelegate(T3));  
  32.             // md();  
  33.             Delegate[] delegates = md.GetInvocationList();  
  34.             for (int i = 0; i < delegates.Length; i++)  
  35.             {  
  36.                 (delegates[i] as MyDelegate)();  
  37.             }  
  38.             Console.ReadKey();  
  39.   
  40.             //Queue<MyDelegate> queue = new Queue<MyDelegate>();  
  41.             //queue.Enqueue(new MyDelegate(T1));  
  42.             //queue.Enqueue(T2);  
  43.             //queue.Enqueue(T3);  
  44.   
  45.   
  46.             //queue.Dequeue();  
  47.             //queue.Dequeue();  
  48.             //queue.Dequeue();  
  49.   
  50.         }  
  51.         static void T1()  
  52.         {  
  53.             Console.WriteLine("ok");  
  54.         }  
  55.         static void T2()  
  56.         {  
  57.             Console.WriteLine("ook");  
  58.         }  
  59.         static void T3()  
  60.         {  
  61.             Console.WriteLine("oook");  
  62.         }  
  63.         static void M1(string msg)  
  64.         {  
  65.             Console.WriteLine(msg);  
  66.         }  
  67.         static void M2(string msg)  
  68.         {  
  69.             Console.WriteLine(msg);  
  70.         }  
  71.         static void M3(string msg)  
  72.         {  
  73.             Console.WriteLine(msg);  
  74.             Console.WriteLine("第3个方法。。");  
  75.         }  
  76.         static void M4(string msg)  
  77.         {  
  78.             Console.WriteLine(msg);  
  79.         }  
  80.         static void M5(string msg)  
  81.         {  
  82.             Console.WriteLine(msg);  
  83.         }  
  84.     }  
  85.   
  86.   
  87. }  
shashou47

发表评论取消回复

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: