单委托:

    

 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 

 

 

相关文章:

  • 2021-12-05
  • 2021-06-08
  • 2021-08-24
  • 2021-08-28
  • 2021-08-28
  • 2022-01-05
  • 2022-12-23
猜你喜欢
  • 2021-08-24
  • 2022-01-27
  • 2022-02-07
  • 2022-12-23
  • 2022-01-21
  • 2021-03-31
  • 2021-12-26
相关资源
相似解决方案