简单的委托示例
单委托:
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
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
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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步