一、定义委托
delegate void StudentDelegate();//【1】定义一个委托
二、定义一个调用和定义事件的类
/// <summary> ///定义事件和调用 事件一定要放在一个类里面 /// </summary> class InvokeDefine //【2】定义一个调用和定义事件的类 { public event StudentDelegate StudentEvent; public void Invoke() { StudentEvent?.Invoke();//?.Null检查运算符 } }
三、订阅者
class EventFunction //【3】订阅者 { public void Student1() { Console.WriteLine("我是订阅者1"); } public void Student2() { Console.WriteLine("我是定订阅者2"); } }
四、订阅事件
static void Main(string[] args) { EventFunction eventFunction = new EventFunction(); InvokeDefine invokeDefine = new InvokeDefine(); invokeDefine.StudentEvent += eventFunction.Student1;//【4】订阅事件 invokeDefine.StudentEvent += eventFunction.Student2; invokeDefine.Invoke(); Console.ReadKey(); }
五、总结
第一点:在定义事件类的外部,他是不能使用=号来操作,只能用+=。
第二点:在定义事件类的外部不能调用 事件。
第三点:事件就是在委托的前面增加一个event关键字。
热门文章
- 被猫咬了流血一定要打免疫球蛋白吗(被猫咬出血一定要打免疫球蛋白吗)
- 3月4日20M/S|SSR/V2ray/Clash/Shadowrocket免费节点每天更新订阅链接,翻墙机场推荐分享
- 动物疫苗接种时间查询app 动物疫苗查询系统
- Vue指令之v-for的使用说明_vue.js
- 动物疫苗去哪里买的啊多少钱(动物疫苗证去哪里办怎么办)
- 宠物养不了该送哪里去无锡(养不了的宠物送去哪里)
- 2月15日21.5M/S|Shadowrocket/Clash/SSR/V2ray免费节点每天更新订阅链接,翻墙机场推荐分享
- 1月10日18.9M/S|Clash/Shadowrocket/SSR/V2ray免费节点每天更新订阅链接,翻墙机场推荐分享
- 布偶难养(布偶难养在哪)
- 1月13日22.3M/S|Shadowrocket/Clash/V2ray/SSR免费节点每天更新订阅链接,翻墙机场推荐分享