事件是用户操作,例如按键、单击、鼠标移动等,或某些事件,例如系统生成的通知。
事件在类中声明和引发,并与在同一类或其他类中使用委托的事件处理程序。包含事件的类用于发布事件。
要在类中声明事件,首先必须声明该事件的委托类型。例如,
public delegate string myDelegate(string str);
现在,声明一个事件 −
event myDelegate newEvent;
现在让我们看一个在C#中处理事件的示例 −
在线演示
using System; namespace Demo { public delegate string myDelegate(string str); class EventProgram { event myDelegate newEvent; public EventProgram() { this.newEvent += new myDelegate(this.WelcomeUser); } public string WelcomeUser(string username) { return "Welcome " + username; } static void Main(string[] args) { EventProgram obj1 = new EventProgram(); string result = obj1.newEvent("My Website!"); Console.WriteLine(result); } } }
Welcome My Website!
以上就是C# 中的事件是什么?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号