c#事件的声明方式

委托字段和为事件声明的委托是不同的

//声明一个委托字段
public delegate double Calc(double x, double y);
//为事件声明的委托类型
public delegate void OrderEventHandler(Customer customer, OrderEventArgs e);

事件的完整声明方式

namespace EventStatement1
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();
            Waiter waiter = new Waiter();
            customer.Order += waiter.Action;//订阅事件
            customer.Action();
            customer.PayTheBill();

        }
    }
    public class OrderEventArgs:EventArgs//传递事件信息的类
    {
        public string DishName { get; set; }
        public string Size { get; set; }
    }

    //事件可以使用委托作为约束
    //委托类型的实例用来记录事件
    //事件依赖于委托类型

    //声明委托,为声明某个事件而准备,后缀EventHandler
    //这个委托是专门用来使用Order事件的
    public delegate void OrderEventHandler(Customer customer, OrderEventArgs e);//两个参数,一个object,一个事件

    //事件的拥有者
    public class Customer
    {
        private OrderEventHandler orderEventHandler;//用来引用事件处理器
        public event OrderEventHandler Order//事件
        {
            add//添加器
            {
                this.orderEventHandler += value;
            }
            remove//移除器
            {
                this.orderEventHandler -= value;
            }
        }
        public double Bill { get; set; }
        public void PayTheBill()
        {
            Console.WriteLine("I will pay ${0}.", this.Bill);
        }
        public void Walkin()
        {
            Console.WriteLine("Walk into the restaurant.");
        }
        public void SitDown()
        {
            Console.WriteLine("Sit down.");
        }
        public void Think()
        {
            if (this.orderEventHandler != null)//触发事件
            {
                OrderEventArgs e = new OrderEventArgs();
                e.DishName = "Chicken";
                e.Size = "large";
                this.orderEventHandler.Invoke(this, e);//通过委托触发了事件
            }
        }
        public void Action()
        {
            Console.ReadLine();
            this.Walkin();
            this.SitDown();
            this.Think();
        }

    }

    //事件的响应者
    public class Waiter
    {
        public void Action(Customer customer, OrderEventArgs e)
        {
            Console.WriteLine("I will serve you the dish {0}.", e.DishName);
            double price = 10;
            switch (e.Size)
            {
                case "small":
                    price = price * 0.5;
                    break;
                case "large":
                    price = price * 1.5;
                    break;
                default:
                    break;
            }
            customer.Bill += price;
        }
    }
}

完整声明方式和简要声明方式的对比

完整声明方式定义委托类型的字段来触发事件
简要声明方式省去了对委托字段的定义,使用事件名来触发事件
这两种方式在功能上没有太大的区别

#完整声明方式#
    public class Customer
    {
        private OrderEventHandler orderEventHandler;//用来引用事件处理器
        public event OrderEventHandler Order//事件
        {
            add//添加器
            {
                this.orderEventHandler += value;
            }
            remove//移除器
            {
                this.orderEventHandler -= value;
            }
        }
    
        public void Think()
        {
            if (this.orderEventHandler != null)//触发事件
            {
                OrderEventArgs e = new OrderEventArgs();
                this.orderEventHandler.Invoke(this, e);//通过委托触发了事件
            }
        }
        public void Action()
        {
            this.Think();
        }
    }

#简要声明方式#
    public class Customer
    {
        //简要声明
        public event OrderEventHandler Order;

        public void Think()
        {
            if (this.Order != null)//触发事件
            {
                OrderEventArgs e = new OrderEventArgs();
                this.Order.Invoke(this, e);//通过委托触发了事件
            }
        }
        public void Action()
        {
            this.Think();
        }
    }

我们可以声明委托类型的字段,为什么还需要事件这种成员?

public的委托字段在类的内部或外部都有可能滥用,因此使用事件这种成员,可以限制程序,使程序更加安全。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值