C#语法:委托与方法

1、了解委托(delegate)

* 委托是一种全新的面向对象语言特性,运行于.NET平台

    *基于委托开发事件驱动程序变得简单

*使用委托可以大大简化多线程编程的难度

2、理解委托

*委托(delegate)可以看成是一种数据类型,它可以定义变量,不过是一种特殊的变量。

*委托定义的变量,可以接受的数值是一个或多个方法,可以理解成它是存放方法的变量,或理解成委托就是一个方法指针。

3、委托的使用方法


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

namespace Csharp控制台练习{
    class Program
    {
        //【1】、声明委托 (定义一个函数的原形:返回值+参数类型和个数)
        public delegate int DemoDelegate(int a, int b);
        //【2】、要存放的方法等具体实现功能(比如加减法)
        static int Add(int a, int b) { return a + b; }
        static int Sub(int a, int b) { return a - b; }

        static void Main(string[] args)
        {
           //【3】、定义委托变量,并且关联要存放于该变量的方法
            DemoDelegate objDel = new DemoDelegate(Add);
           //【4】、通过委托调用方法,而不是直接调用方法
            Console.WriteLine( objDel(10, 20));
            objDel -= Add;
            objDel += Sub;
            Console.WriteLine( objDel(10, 20));
            
            Console.ReadKey();
        }

        

    }
}

此程序输出结果为30     -10.

使用委托的步骤:

1、声明委托:关键字delegate + 返回值 +委托名 +参数 。 返回值和参数怎么确定? 当然是要和存放的方法类型要一致了

2、委托对象的定义。

3、将委托与方法关联起来,除了在创建对象的时候关联方法也可以通过  “ += " 绑定方法,也可以通过 ”-=“ 方法解绑 来实现方法的关联。

4、通过委托调用方法。


以上就是委托关联方法的具体步骤。但是上述代码并不能体现委托的真正用处。本来用方法就能实现的功能,搞得那么复杂不是闲得慌?

委托的用途十分多,其中一个就是可以实现窗体之间的通信,或者说是数据传递。现在要实现下述功能:有多个窗体,一个是主窗体,和其他是从窗体,主窗体中有一个单击按钮,而在每个从窗体中同步显示单击按钮的次数。要知道,一个窗体无法直接调用另一个窗体的方法,也无法直接操作另一个窗体的控件属性,通过委托便可以实现。

1、从窗体代码(frmOther1.)

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 Csharp窗体练习
{
    public partial class frmOther1 : Form
    {
        public frmOther1()
        {
            InitializeComponent();
        }
        public void Receive(string counter)
        {
            labCounter.Text = counter;
        }
      
    }
}


每个从窗体布局如图:只有一个label(name:labCounter)控件,代码中只多了一个Receive方法。

主窗体布局如图:一个单击按钮(btnCounter)一个复位按钮(btnClear).


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 Csharp窗体练习
{
    public delegate void ShowCounterDele(string counter);//【1】声明个委托,一般在类外面声明
   
    public partial class Form1 : Form
    {
        ShowCounterDele showCounterdel;                 //【2】定义委托对象
        
     
        public Form1()
        {
            
            InitializeComponent();
          
            frmOther frmother = new frmOther();
            frmOther1 frmother1 = new frmOther1();
            frmOther2 frmother2 = new frmOther2();


            showCounterdel += frmother.Receive;         //【3】将委托对象与方法关联起来
            showCounterdel += frmother1.Receive;
            showCounterdel += frmother2.Receive;

            frmother.Show();
            frmother1.Show();
            frmother2.Show();
            
        }


        private int count = 0;
        private void btnCounter_Click(object sender, EventArgs e)
        {
          
            count++;
            showCounterdel(count.ToString());          //【4】、通过委托调用方法
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            count=0;
            showCounterdel(count.ToString());
        }
    }
}

执行上面程序,点击主窗体中的按钮,从窗体会显示单击次数。点击主窗体的复位按钮然后计数归零。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值