C#委托:事件驱动编程的基石

目录

了解委托

委托使用的基本步骤

声明委托(定义一个函数的原型:返回值 + 参数类型和个数)

根据委托定义的函数原型编写需要的方法

创建委托对象,关联“具体方法”

通过委托调用方法,而不是直接使用方法

委托对象所关联的方法可以动态变化

委托应用场景


了解委托

  • 委托是一种全新的面向对象的特性,运行在.Net平台
  • 基于委托,开发事件驱动程序变得非常简单
  • 使用委托可以大大简化多线程编程难点

委托使用的基本步骤

  1. 声明委托(定义一个函数的原型:返回值 + 参数类型和个数)

  2. 根据委托定义的函数原型编写需要的方法

  3. 创建委托对象,关联“具体方法”

  4. 通过委托调用方法,而不是直接使用方法

  5. 委托对象所关联的方法可以动态变化

委托对象的声明一般是放在类外面。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //[3]创建委托对象,关联"具体方法"
            CalculatorDelegate objCal = new CalculatorDelegate(Add);
            //[4]通过委托去调用方法,而不是直接使用方法
            int result = objCal(10, 20);
            Console.WriteLine("10 + 20 = {0}", result);
            objCal -= Add; //断开当前委托对象关联的方法
            objCal += Sub; //重新指向一个新的方法(减法)
            result = objCal(10, 20); //重新使用委托对象,完成减法功能
            Console.WriteLine("10 - 20 = {0}", result);
            Console.ReadLine();
        }
        //[2]根据委托对象创建一个"具体方法"实现加法功能
        static int Add(int a, int b)
        {
            return a + b;
        }
        //[2]根据委托对象创建一个"具体方法"实现减法功能
        static int Sub(int a, int b)
        {
            return a - b;
        }
    }
    //[1]声明委托(定义一个函数的原型:返回值 + 参数返回个数和类型)
    public delegate int CalculatorDelegate(int a, int b);
}

委托应用场景

利用委托实现主窗体和从窗体之间传值

主窗体FrmMain.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DelegateDemo3
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            FrmOther objFrm = new FrmOther();
            //将从窗体的委托变量和主窗体对应的方法关联
            objFrm.msgSender = this.Receiver;
            objFrm.Show();
        }
        /// <summary>
        /// 接收委托传递的信息
        /// </summary>
        /// <param name="counter"></param>
        public void Receiver(string counter)
        {
            this.lblShow.Text = counter;
        }
    }
    //委托声明
    public delegate void ShowCounter(string counter);
}

从窗体FrmOther.cs 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DelegateDemo3
{
    public partial class FrmOther : Form
    {
        public FrmOther()
        {
            InitializeComponent();
        }
        //根据委托创建委托对象
        public ShowCounter msgSender;
        //计数
        private int counter = 0;
        private void btnClick_Click(object sender, EventArgs e)
        {
            counter++;
            if (msgSender != null)
            {
                msgSender(counter.ToString());
            }
        }
    }

}

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值