C#自定义事件的案例

方法一,详细的声明

cs 复制代码
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;
        }
    }
}

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

cs 复制代码
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;
        }
    }
}
相关推荐
mudtools16 小时前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
侃侃_天下21 小时前
最终的信号类
开发语言·c++·算法
echoarts21 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix1 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
大飞pkz1 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
每天回答3个问题1 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说1 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔1 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号1 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_1 天前
QT(4)
开发语言·汇编·c++·qt·算法