C#-----委托delegate的定义与使用

    委托是一种数据结构,它引用静态方法或引用类实例及该类的实例方法

    委托的声明: delegate <函数返回类型> <委托名> (<函数参数>)

//声明委托
delegate int MyDelegate(int x, int y);

    注册函数

static int GetSum(int x, int y)
{
      return x + y;
}

使用new关键字

    实例化声明: <委托类型> <实例化名>=new <委托类型>(<注册函数>)

//使用new关键字
MyDelegate _myDelegate = new MyDelegate(GetSum);
Console.WriteLine(_myDelegate(2,3));

使用匿名方法

    实例化声明: <委托类型> <实例化名>=delegate(<函数参数>){函数体};

//使用匿名方法
MyDelegate myDelegate = delegate (int x, int y)
{
     return x + y;
};
Console.WriteLine(myDelegate(4, 3));

使用Lambda表达式

    例:MyDelegate myDelegateLambda = (int x, int y) => { return x + y; };

//使用Lambda表达式
MyDelegate myDelegateLambda = (int x, int y) => { return x + y; };
Console.WriteLine(myDelegateLambda(6, 3));

 使用new实例化委托Demo

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

namespace ConsoleApp2
{
    public class DelegatePractise
    {
        public delegate string DelegatePrintInfoFirst(string userName, string userPwd);
        /// <summary>
        /// 有参数有返回值
        /// </summary>
        public DelegatePrintInfoFirst delegatePrintInfoFirst = null;

        public delegate void DelegatePrintInfoSecond(string userName, string userPwd);
        /// <summary>
        /// 有参数无返回值
        /// </summary>
        public DelegatePrintInfoSecond delegatePrintInfoSecond = null;

        public delegate void DelegatePrintInfoThree(string userName, string userPwd);
        /// <summary>
        /// 参数传递
        /// </summary>
        public DelegatePrintInfoThree delegatePrintInfoThree = null;
        public DelegatePractise()
        {
            //委托绑定方法
            delegatePrintInfoFirst +=new DelegatePrintInfoFirst(printInfoFirst) ;
            delegatePrintInfoSecond = new DelegatePrintInfoSecond(printInfoSecond);
        }

        /// <summary>
        /// 有参数有返回值
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPwd"></param>
        /// <returns></returns>
        public string printInfoFirst(string userName, string userPwd)
        {
            return "有参数有返回值," + "用户名:" + userName + ",密码:" + userPwd;
        }

        /// <summary>
        /// 有参数无返回值
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPwd"></param>
        public void printInfoSecond(string userName, string userPwd)
        {
            Console.WriteLine("有参数无返回值," + "用户名:" + userName + ",密码:" + userPwd);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static ConsoleApp2.DelegatePractise;

namespace ConsoleApp2
{
    public class ParentDelegate
    {
        public DelegatePractise delegatePractise = null;
        public ParentDelegate()
        {
            delegatePractise = new DelegatePractise();
            //将DelegatePractise对象绑定委托方法ShowParams
            delegatePractise.delegatePrintInfoThree += new DelegatePrintInfoThree(ShowParams);
        }

        public void ShowParams(string userName, string userPwd)
        {
            Console.WriteLine("用户名:" + userName + ",密码:" + userPwd);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建ParentDelegate
            ParentDelegate parentDelegate = new ParentDelegate();
            //创建DelegatePractise
            DelegatePractise delegatePractise = new DelegatePractise();
            Console.WriteLine(delegatePractise.delegatePrintInfoFirst("jtx", "123456"));
            delegatePractise.delegatePrintInfoSecond("jtx", "123456");
            //在父窗体打印
            parentDelegate.delegatePractise.delegatePrintInfoThree("test", "123456");
            Console.ReadLine();
        }
    }
}

控制台输出结果:

有参数有返回值,用户名:jtx,密码:123456
有参数无返回值,用户名:jtx,密码:123456
用户名:test,密码:123456

    注意:event事件只能从声明它们的类中调用,派生类不能直接调用在基类声明的事件

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

namespace delegatePractise
{
    public class DelegatePractise
    {
        public delegate string DelegatePrintInfoFirst(string userName, string userPwd);
        public event DelegatePrintInfoFirst delegatePrintInfoFirst = null;

        public DelegatePractise()
        {
            //委托绑定方法
            delegatePrintInfoFirst += printInfoFirst;
        }

        public void callMethods()
        {
            Console.WriteLine(delegatePrintInfoFirst("jtx", "123456"));
        }

        public string printInfoFirst(string userName, string userPwd)
        {
            return "用户名:" + userName + ",密码:" + userPwd;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace delegatePractise
{
    class Program
    {
        static void Main(string[] args)
        {
            //同步调用,它将阻塞当前线程,调用完毕后再继续向下进行
            DelegatePractise delegatePractise = new DelegatePractise();
            delegatePractise.callMethods();
            Console.ReadLine();
        }
    }
}
原文地址:https://www.cnblogs.com/fengfuwanliu/p/10867561.html