委托

  //委托是装方法的容器,先弄个容器,通过这个容器+=-+来装或者减方法。然后就可以把委托作为参数进行传递了。委托是一个类型。

委托本质是一个密封类。委托用于将方法作为方法的参数进行传递。

//当委托装入多个具有返回值的方法时,调用时,会返回最后一个装入的方法的返回值。若需要得到所有方法返回值。需要getInvocationList()

方法,得到一个delegate数组,数组元素就是加入的方法的委托。只需要遍历输出返回值。

delegate string vocaldelegate();//声明一个返回值string无参数委托;

vocaldelegate vdg = new vocaldelegate(Lising);
vdg += Zhousing;
Delegate[] singers = vdg.GetInvocationList();
foreach (vocaldelegate item in singers)
{
string cookfood = item();
MessageBox.Show(cookfood);
}

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

namespace Delegate
{
    delegate void bbqdelegate(string somefood);
    class Program
    {
        static void bbqChina(string somefood)
        {
            Console.WriteLine(somefood + "加点孜然");
        }
        static void bbqEnglish(string somefood)
        
        {
            Console.WriteLine(somefood + "加点芥末");
        }
        //各国做羊肉串的方法//参数需要一个bbq的方法,委托就是方法的容器,装的就是方法
      static  void makeChuan(bbqdelegate bbq)
        {
            string food = "羊肉";
            bbq.Invoke(food);
        }
        static void Main(string[] args)
        {
            bbqdelegate bbq = new bbqdelegate(bbqChina);
            bbq += bbqEnglish;
            makeChuan(bbq);
            Console.ReadKey();
        }
    }
}
委托作为方法参数

 event关键字对委托进行封装的过程

自定义类继承自

System.Windows.Forms.Button形成自定义三击控件。自定义控件逻辑代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 委托
{
    delegate void usercontrol();
    class DelEvent:System.Windows.Forms.Button
    {
        public DelEvent() {
            base.Click += DelEvent_Click;
        }

        int countnum = 0;
        //字段永远不要public
       // public usercontrol threecontrol;

        //所以我用私有字段加public方法封装
        //private usercontrol threecontrol;
        //public void addthree(usercontrol d)
        //{
        //    threecontrol += d;
        //}

        //以下使用event关键字封装
        //private usercontrol threecontrol;
        //public event usercontrol threecontrolevent
        //{
        //    add { threecontrol += value; }
        //    remove { threecontrol -= value; }
        //}

        //然后就简化成一句话
        public event usercontrol threecontrol;
void DelEvent_Click(object sender, EventArgs e) { countnum++; if (countnum >= 3) { MessageBox.Show("三击默认处理方案"); countnum = 0; if(threecontrol!=null) threecontrol.Invoke(); } } } }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 委托
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DelEvent threeControl = new DelEvent();
            threeControl.Text = "三枪";
            threeControl.Size = new Size(100, 30);
            threeControl.Location = new Point(162, 50);
            //这里使用+=来给事件绑定方法  event关键字修饰了委托
            //实际是对委托进行了封装(阉割)
            threeControl.threecontrol += threeClick;
            this.Controls.Add(threeControl);
        }
        //三击触发的方法
        void threeClick() {
            MessageBox.Show("这是点了三下发生的业务逻辑");
        }
客户端逻辑
原文地址:https://www.cnblogs.com/zhanying/p/4119544.html