一个简单的小例子让你明白c#中的委托

本文通过主持人角色发布问题,多个嘉宾响应的方式,详细解析了C#中事件的实现机制。通过委托链和事件处理,展示了如何利用C#事件进行异步编程和消息传递。文章深入探讨了事件的定义、触发过程以及嘉宾(类)如何响应这些事件,提供了一个全面理解C#事件系统应用的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


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

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



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


QuestionArgs.cs

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



Program.cs

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


Host.cs

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


Guests.cs

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


GuestC.cs

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


GuestB.cs

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


GuestA.cs

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


运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值