c#委托事件整理

1.委托从字面上理解就是一种代理,类似于房屋中介,由租房人委托中介为其租赁房屋;

   委托是一种引用类型,虽然在定义委托时与方法有些相似,但不能将其称为方法。

   下面定义一个int类型两个int参数的委托;

        /// <summary>
        /// 定义委托:返回类型int,两个参数为int
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public delegate int Fun(int a, int b);

 

 

2.定义一个int类型两个int参数的方法

        public static int Add(int a, int b)
        {
            return a + b;
        }

3.实例化委托

            Fun f = new Fun(Add);
            int result = f.Invoke(1, 2);
            /* 简写
            Fun f = Add;
            int result = f(1, 2);
            */
            MessageBox.Show("和:"+result.ToString());

事件

1. 事件是一种引用类型,实际上也是一种特殊的委托。

    通常,每一个事件的发生都会产生发送方和接收方,发送方是指引发事件的对象,接收方则是指获取、处理事件。事件要与委托一起使用。

2.事件参数

public class PriceChangeEventArgs : EventArgs
    {
        /// <summary>
        /// 原价
        /// </summary>
        public readonly decimal OldPrice;
        /// <summary>
        /// 现价
        /// </summary>
        public readonly decimal NewPrice;

        public PriceChangeEventArgs(decimal oldPrice, decimal newPrice)
        {
            OldPrice = oldPrice;
            NewPrice = newPrice;
        }
    }

3.事件

/// <summary>
    /// 示例:商品单价变更触发事件
    /// 日期:2020-08-14
    /// 作者:Dr
    /// </summary>
    class Goods
    {
        /// <summary>
        /// 单价
        /// </summary>
        private decimal price = 0;
        public decimal Price
        {
            get { return price; }
            set
            {
                if(price==value)
                {
                    price = value;
                    return;
                }
                OnPriceChangeHandler(new PriceChangeEventArgs(price, value));
                price = value;
            }
        }
        /// <summary>
        /// 价格变更触发事件
        /// </summary>
        public event EventHandler<PriceChangeEventArgs> PriceChangeHandler;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnPriceChangeHandler(PriceChangeEventArgs e)
        {
            if (PriceChangeHandler != null)
            {
                PriceChangeHandler.Invoke(this, e);
            }
            /*简写
            PriceChangeHandler?.Invoke(this, e);
            */
        }
    }

4.调用事件

Goods g;
        public Form1()
        {
            InitializeComponent();
            g = new Goods();
            //价格变更触发事件
            g.PriceChangeHandler += G_PriceChangeHandler;            
        }

        private void G_PriceChangeHandler(object sender, PriceChangeEventArgs e)
        {
            MessageBox.Show("原价:" + e.OldPrice + "现价:" + e.NewPrice);
        }
        private void button2_Click(object sender, EventArgs e)
        {       
            g.Price = this.numericUpDown1.Value;
        }

大哥下载直通车

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值