标签:
代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace 委托_例子 8 { 9 static class Program 10 { 11 delegate void Show(string name); 12 13 static void CatShow(string name) 14 { 15 Console.WriteLine("Name:" + name); 16 } 17 18 static void DogShow(string name) 19 { 20 Console.WriteLine("Name:" + name); 21 } 22 23 static void LionShow(string name) 24 { 25 Console.WriteLine("Name:" + name); 26 } 27 28 static void Animal(Show s, string name) 29 { 30 s(name); 31 } 32 33 static void Main(string[] args) 34 { 35 //多播委托 36 //通过+=运算符想多播委托中订阅方法 37 Show s = new Show(CatShow); 38 s += new Show(DogShow); 39 s += new Show(LionShow); 40 41 //通过-=运算符向多播委托中取消方法 42 s -= new Show(LionShow); 43 44 Program.Animal(s, "KKK"); 45 46 Console.ReadKey(); 47 } 48 } 49 }
标签:
原文地址:http://www.cnblogs.com/KTblog/p/4525948.html