简单的委托示例

单委托:

    

 1 using System;
 2 namespace test
 3 {
 4     public delegate void saydel(string name);
 5     class Hello
 6         {
 7             public void sc(string name)
 8             {
 9                 string str="你好,"+name;
10                 Console.WriteLine(str);
11             }
12             public void se(string name)
13             {
14                 string str="hello,"+name;
15                 Console.WriteLine(str);
16             }
17             public void DoWork(string name,saydel ms)
18             {
19                 ms(name);
20             }
21             static void Main()
22                 {
23                     Hello h=new Hello();
24                     h.DoWork("user",h.se);
25                     h.DoWork("user",h.sc);
26                 }
27         }
28 }
29 

 

 

 

多播委托:

 

 1 using System;
 2 namespace test
 3 {
 4     public delegate void saydel(string name);
 5     class Hello
 6         {
 7             public void sc(string name)
 8             {
 9                 string str="你好,"+name;
10                 Console.WriteLine(str);
11             }
12             public void se(string name)
13             {
14                 string str="hello,"+name;
15                 Console.WriteLine(str);
16             }
17             public void DoWork(string name,saydel ms)
18             {
19                 ms(name);
20             }
21             static void Main()
22                 {
23                     Hello h=new Hello();
24                     saydel deg=h.sc;
25                     deg+=h.se;
26                     h.DoWork("user",deg);
27                 }
28         }
29 }
30 

 

 

posted @ 2010-03-31 17:12  HeartLight  阅读(302)  评论(0编辑  收藏  举报