当前位置: 首页 > news >正文

C#自定义事件的案例

方法一,详细的声明

namespace HelloWorldConsole
{internal class Program{static void Main(string[] args){Customer customer = new Customer();Waiter waiter = new Waiter();customer.Order += waiter.Action;customer.Action();}}public class OrderEventArgs : EventArgs{public string DishName { get; set; }public string Size { get; set; }}public delegate void OrderEventHandler(Customer customer, OrderEventArgs e);public class Customer{public double Bill { get; set; }private OrderEventHandler orderEventHandler;public event OrderEventHandler Order{add { orderEventHandler += value; }remove { orderEventHandler -= value; }}public void PayTheBill(){Console.WriteLine("i will pay-{0}", Bill);}public void Think(){if (orderEventHandler != null){OrderEventArgs e = new OrderEventArgs();e.DishName = "宫保鸡丁";e.Size = "large";orderEventHandler(this, e);}}public void Action(){Think();PayTheBill();}}public class Waiter{public void Action(Customer customer, OrderEventArgs e){double price = 10;switch (e.Size){case "small": price /= 2; break;case "large": price *= 1.5; break;default: break;}Console.WriteLine("i will serve the dish {0}-{1}", e.DishName, e.Size);customer.Bill += price;}}
}

方法二,简化的声明,也是最常用的写法

namespace HelloWorldConsole
{internal class Program{static void Main(string[] args){Customer customer = new Customer();Waiter waiter = new Waiter();customer.Order += waiter.Action;customer.Action();}}/// <summary>/// 事件参收/// </summary>public class OrderEventArgs : EventArgs{public string DishName { get; set; }public string Size { get; set; }}public class Customer{public double Bill { get; set; }public event EventHandler Order;public void Think(){if (Order != null){OrderEventArgs e = new OrderEventArgs();e.DishName = "宫保鸡丁";e.Size = "large";Order(this, e);}}public void PayTheBill(){Console.WriteLine("i will pay-{0}", Bill);}public void Action(){Think();PayTheBill();}}public class Waiter{public void Action(object sender, EventArgs _e){Customer customer = sender as Customer;OrderEventArgs e = _e as OrderEventArgs;double price = 10;switch (e.Size){case "small": price /= 2; break;case "large": price *= 1.5; break;default: break;}Console.WriteLine("i will serve the dish {0}-{1}", e.DishName, e.Size);customer.Bill += price;}}
}


http://www.mrgr.cn/news/61307.html

相关文章:

  • Thrustmaster Hotas Warthog飞行操作杆开发
  • 7 分布式定时任务调度框架
  • 44.ComboBox的数据绑定 C#例子 WPF例子
  • Spring 项目 基于 Tomcat容器进行部署
  • Vue2+OpenLayers添加/删除点、点击事件功能实现(提供Gitee源码)
  • VSCode 中的 launch.json 配置使用
  • XD6500S替代翱捷ASR6500S系列低功耗lora SPI模块SX1262/SX1278
  • Spring之HTTP客户端--RestTemplate的使用
  • 揭秘抖音SEO矩阵源码,霸屏秘籍!
  • bean生命周期
  • 煤矿安全大模型:微调internlm2模型实现针对煤矿事故和煤矿安全知识的智能问答
  • 2、片元着色器之有向距离场(SDF)运算:并集、差集、交集
  • 【JavaScript】入门详解
  • 化繁为简的鸿蒙原生开发时代,“开发者”被高度关注
  • 豆瓣同城活动采集-过去一年到未来已定档活动
  • Video-XL:智源研究院开源超基准测试的长视频理解大模型
  • [ACTF2020 新生赛]Exec 1
  • 2. Flink快速上手
  • 如何通过接口版本控制实现向后兼容
  • autojs使用中的一些坑
  • 看低代码开发如何通过几步加速融入产业进程
  • SAP-MM委外订单的退货处理
  • FreeRTOS队列分析
  • M3U8不知道如何转MP4?包能学会的4种格式转换教学!
  • StringBuilder类
  • golang版本工具GVM 和包管理工具go mod原理讲解