我希望你是我独家记忆

一段永远封存的记忆,随风而去
posts - 263, comments - 31, trackbacks - 0, articles - 3
   :: 首页 :: 新随笔 ::  :: 聚合  :: 管理

委托实例2

Posted on 2009-07-30 14:20 Hero 阅读(219) 评论(0)  编辑 收藏 引用 所属分类: C#积累
 1 using System;
 2 
 3 //委托 -- 就是函数指针列表 -- 可以做容器,也可以做参数
 4 delegate void MyDelegate(string s);
 5 
 6 class MyClass
 7 {
 8     public static void Hello(string s)
 9     {
10         Console.WriteLine("  Hello, {0}!", s);
11     }
12 
13     public static void Goodbye(string s)
14     {
15         Console.WriteLine("  Goodbye, {0}!", s);
16     }
17 
18     public static void Main()
19     {
20         MyDelegate a, b, c, d;
21 
22         // 创建引用 Hello 方法的 
23         // 委托对象 a:
24         a = new MyDelegate(Hello);
25         // 创建引用 Goodbye 方法的 
26         // 委托对象 b:
27         b = new MyDelegate(Goodbye);
28         // a 和 b 两个委托组成 c, 
29         // c 将按顺序调用这两个方法:
30         c = a + b;
31         // 从组合委托中移除 a 而保留 d, 
32         // 后者仅调用 Goodbye 方法:
33         d = c - a;
34 
35         Console.WriteLine("Invoking delegate a:");
36         a("A");
37         Console.WriteLine("Invoking delegate b:");
38         b("B");
39         Console.WriteLine("Invoking delegate c:");
40         c("C");
41         Console.WriteLine("Invoking delegate d:");
42         d("D");
43     }
44 }
45 
46 

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理