C# 关于类的事件和委托
事件是C#中的又一个重要概念,在发生其他类或对象需要关注的事情时,
本类或对象可以通过事件来通知它们。
发送事件的类称为事件的发送者,而接受事件的类称为事件的订阅户。
C# 委托
委托使用的关键字为delegate。
class Program { public delegate void Mypaly(string str); //声明委托 class Play { private string game; public Play(string s) { game = s; } public void PlayStart(string name) //委托调用的方法1 { Console.WriteLine("“{0}”开始玩《{1}》!", name, game); } public void PlayClose(string name) //委托调用的方法2 { Console.WriteLine("“{0}”已关掉《{1}》!", name, game); } public void callMypaly(Mypaly d, string s) //调用委托中的方法 { d(s); } } static void Main(string[] args) { Play paly = new Play("魔兽争霸"); Mypaly mypalyStart = new Mypaly(paly.PlayStart); Mypaly mypalyClose = new Mypaly(paly.PlayClose); Mypaly mypaly = mypalyStart + mypalyClose; mypaly("张三"); //paly.callMypaly(mypalyStart, "张三"); //paly.callMypaly(mypalyClose, "张三"); Console.ReadKey(); } }
打印结果:
“张三”开始玩《魔兽争霸》!
“张三”已关掉《魔兽争霸》!
C# 事件中的委托
声明事件的关键字为event。
class Program { class Eventclass //先定义一个类 { public delegate void CustomEventHandler(object sender, EventArgs e); //⒈定义一个委托类型的对象 //用delegate数据类型声明事件,要用event关键字 public event CustomEventHandler CustomEvent; public event CustomEventHandler Load; public void InCustomEvent() //引用事件方法 { CustomEvent(this, EventArgs.Empty); } public void onLoad() { temp1: Console.BackgroundColor = ConsoleColor.Red; Load(this, EventArgs.Empty); string s = Console.ReadLine(); if (s != "yuping") { Console.WriteLine("You must type 'yuping' for change it !"); goto temp1; } else { Console.BackgroundColor = System.ConsoleColor.Black; Console.Clear(); } } public void CustomEvent1(object sender, EventArgs e) { Console.WriteLine("Fire Event"); } public void Load1(object sender, EventArgs e) { Console.WriteLine("Current background color is {0}. Please input:", System.Console.BackgroundColor.ToString()); } } static void Main(string[] args) { Eventclass myEventclass = new Eventclass(); //实例Eventclass类 myEventclass.CustomEvent += new Eventclass.CustomEventHandler(myEventclass.CustomEvent1); //⒉实例关联事件 myEventclass.InCustomEvent(); //⒊调用 myEventclass.Load += new Eventclass.CustomEventHandler(myEventclass.Load1); myEventclass.onLoad(); Console.ReadKey(); } }
打印结果:
Fire Event
Current background color is Red. Please input:
abc
You must type 'yuping' for change it !
Current background color is Red. Please input:
//举例:通过在Class2中定义并触发事件,在Class1中调用事件处理程序。 class Program { public class MyEventArgs : EventArgs //继承EventArgs 包含事件数据的类的基类 { public string str; } public delegate void MyEventHandler1(object sender, MyEventArgs e); //声明委托对象 class Class2 { public event MyEventHandler1 Event1; //定义事件 public void mEvent1(MyEventArgs e) //触发事件 { if (Event1 != null) { Event1(this, e); } } } class Class1 { public Class1(Class2 class2) //创建委托对象并包含事件处理函数 { MyEventHandler1 mh1 = new MyEventHandler1(Method1); class2.Event1 += mh1; //订阅事件 这里只能+= 或-= } public void Method1(object sender, MyEventArgs e) { Console.WriteLine("事件处理结果:" + e.str); } } static void Main(string[] args) { Class2 class2 = new Class2(); Class1 class1 = new Class1(class2); MyEventArgs my1 = new MyEventArgs(); my1.str = "aaa"; class2.mEvent1(my1); Console.ReadKey(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?