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;
        }
    }
}
相关推荐
tobebetter95274 小时前
How to manage python versions on windows
开发语言·windows·python
9***P3345 小时前
PHP代码覆盖率
开发语言·php·代码覆盖率
CoderYanger5 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
jllllyuz5 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
多多*5 小时前
Java复习 操作系统原理 计算机网络相关 2025年11月23日
java·开发语言·网络·算法·spring·microsoft·maven
p***43486 小时前
Rust网络编程模型
开发语言·网络·rust
ᐇ9596 小时前
Java集合框架深度实战:构建智能教育管理与娱乐系统
java·开发语言·娱乐
梁正雄6 小时前
1、python基础语法
开发语言·python
强化学习与机器人控制仿真7 小时前
RSL-RL:开源人形机器人强化学习控制研究库
开发语言·人工智能·stm32·神经网络·机器人·强化学习·模仿学习
百***48077 小时前
【Golang】slice切片
开发语言·算法·golang