一个简单的小例子让你明白c#中的委托-终于懂了!

时间:2022-08-19 18:07:14

模拟主持人发布一个问题,由多个嘉宾来回答这个问题。

分析:从需求中抽出Host (主持人) 类和Guests (嘉宾) 类。

作为问题的发布者,Host不知道问题如何解答。因此它只能发布这个事件,将事件委托给多个嘉宾去处理。因此在Host 类定义事件,在Guests类中定义事件的响应方法。通过多番委托的"+="将响应方法添加到事件列表中,最终 Host 类将触发这个事件。实现过程如下:

一个简单的小例子让你明白c#中的委托-终于懂了!

代码其实很少下面贴出来所有代码:

QuestionArgs.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. public class QuestionArgs:EventArgs
  8. {
  9. public string Message { get; set; }
  10. }
  11. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. public class QuestionArgs:EventArgs
  8. {
  9. public string Message { get; set; }
  10. }
  11. }

Program.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Host host = new Host();
  12. host.Name = "主持人";
  13. host.args.Message = "C#的事件如何实现的?";
  14. Guests[] gArray = new Guests[3]
  15. {
  16. new GuestA(){Name = "张小三"},
  17. new GuestB(){Name = "李小四"},
  18. new GuestC(){Name = "王老五"}
  19. };
  20. //用+=号,将嘉宾的答题方法加入到委托链
  21. host.QuestionEvent += new QuestionHandler(gArray[0].answer);
  22. host.QuestionEvent += new QuestionHandler(gArray[1].answer);
  23. host.QuestionEvent += new QuestionHandler(gArray[2].answer);
  24. //触发事件
  25. host.StartAnswer();
  26. Console.ReadLine();
  27. }
  28. }
  29. }<span style="color:#ff0000;">
  30. </span>
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Host host = new Host();
  12. host.Name = "主持人";
  13. host.args.Message = "C#的事件如何实现的?";
  14. Guests[] gArray = new Guests[3]
  15. {
  16. new GuestA(){Name = "张小三"},
  17. new GuestB(){Name = "李小四"},
  18. new GuestC(){Name = "王老五"}
  19. };
  20. //用+=号,将嘉宾的答题方法加入到委托链
  21. host.QuestionEvent += new QuestionHandler(gArray[0].answer);
  22. host.QuestionEvent += new QuestionHandler(gArray[1].answer);
  23. host.QuestionEvent += new QuestionHandler(gArray[2].answer);
  24. //触发事件
  25. host.StartAnswer();
  26. Console.ReadLine();
  27. }
  28. }
  29. }<span style="color:#ff0000;">
  30. </span>

Host.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. public delegate void QuestionHandler(object sender,QuestionArgs e);
  8. public class Host
  9. {
  10. //定义一个事件
  11. public event QuestionHandler QuestionEvent;
  12. public QuestionArgs args { set; get; }
  13. public Host()
  14. {
  15. //初始化事件参数
  16. args = new QuestionArgs();
  17. }
  18. public string Name { get; set; }
  19. public void StartAnswer()
  20. {
  21. Console.WriteLine("开始答题");
  22. QuestionEvent(this, args);
  23. }
  24. }
  25. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. public delegate void QuestionHandler(object sender,QuestionArgs e);
  8. public class Host
  9. {
  10. //定义一个事件
  11. public event QuestionHandler QuestionEvent;
  12. public QuestionArgs args { set; get; }
  13. public Host()
  14. {
  15. //初始化事件参数
  16. args = new QuestionArgs();
  17. }
  18. public string Name { get; set; }
  19. public void StartAnswer()
  20. {
  21. Console.WriteLine("开始答题");
  22. QuestionEvent(this, args);
  23. }
  24. }
  25. }

Guests.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. /// <summary>
  8. /// 父类
  9. /// </summary>
  10. public class Guests
  11. {
  12. /// <summary>
  13. /// 嘉宾姓名
  14. /// </summary>
  15. public string Name { get; set; }
  16. public virtual void answer(object sender, QuestionArgs e)
  17. {
  18. Console.Write("事件的发出者:" + (sender as Host).Name);
  19. Console.WriteLine("问题是:" + e.Message);
  20. }
  21. }
  22. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. /// <summary>
  8. /// 父类
  9. /// </summary>
  10. public class Guests
  11. {
  12. /// <summary>
  13. /// 嘉宾姓名
  14. /// </summary>
  15. public string Name { get; set; }
  16. public virtual void answer(object sender, QuestionArgs e)
  17. {
  18. Console.Write("事件的发出者:" + (sender as Host).Name);
  19. Console.WriteLine("问题是:" + e.Message);
  20. }
  21. }
  22. }

GuestC.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. class GuestC:Guests
  8. {
  9. public override void answer(object sender, QuestionArgs e)
  10. {
  11. base.answer(sender, e);
  12. Console.WriteLine("{0}开始答题:我不知道", this.Name);
  13. }
  14. }
  15. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. class GuestC:Guests
  8. {
  9. public override void answer(object sender, QuestionArgs e)
  10. {
  11. base.answer(sender, e);
  12. Console.WriteLine("{0}开始答题:我不知道", this.Name);
  13. }
  14. }
  15. }

GuestB.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. class GuestB:Guests
  8. {
  9. public override void answer(object sender, QuestionArgs e)
  10. {
  11. base.answer(sender, e);
  12. Console.WriteLine("{0}开始答题:我不知道", this.Name);
  13. }
  14. }
  15. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. class GuestB:Guests
  8. {
  9. public override void answer(object sender, QuestionArgs e)
  10. {
  11. base.answer(sender, e);
  12. Console.WriteLine("{0}开始答题:我不知道", this.Name);
  13. }
  14. }
  15. }

GuestA.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. class GuestA:Guests
  8. {
  9. public override void answer(object sender, QuestionArgs e)
  10. {
  11. base.answer(sender, e);
  12. Console.WriteLine("{0}开始答题:我不知道", this.Name);
  13. }
  14. }
  15. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace EventDemo
  6. {
  7. class GuestA:Guests
  8. {
  9. public override void answer(object sender, QuestionArgs e)
  10. {
  11. base.answer(sender, e);
  12. Console.WriteLine("{0}开始答题:我不知道", this.Name);
  13. }
  14. }
  15. }

运行结果:

一个简单的小例子让你明白c#中的委托-终于懂了!