C#-----委托delegate的定义与使用
委托是一种数据结构,它引用静态方法或引用类实例及该类的实例方法
委托的声明: delegate <函数返回类型> <委托名> (<函数参数>)
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。//声明委托 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 delegatePractise { 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 string DelegatePrintInfoThree(); /// <summary> /// 无参数有返回值 /// </summary> public DelegatePrintInfoThree delegatePrintInfoThree = null; public delegate void DelegatePrintInfoFour(); /// <summary> /// 无参数无返回值 /// </summary> public DelegatePrintInfoFour delegatePrintInfoFour = null; public DelegatePractise() { //委托绑定方法 delegatePrintInfoFirst += printInfoFirst; delegatePrintInfoSecond = new DelegatePrintInfoSecond(printInfoSecond); delegatePrintInfoThree = new DelegatePrintInfoThree(printInfoThree); delegatePrintInfoFour = new DelegatePrintInfoFour(printInfoFour); } /// <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); } /// <summary> /// 无参数有返回值 /// </summary> /// <returns></returns> public string printInfoThree() { return "无参数有返回值"; } /// <summary> /// 无参数无返回值 /// </summary> public void printInfoFour() { Console.WriteLine("无参数无返回值"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace delegatePractise { class Program { //声明委托 delegate int MyDelegate(int x, int y); static void Main(string[] args) { DelegatePractise delegatePractise = new DelegatePractise(); Console.WriteLine(delegatePractise.delegatePrintInfoFirst("jtx", "123456")); delegatePractise.delegatePrintInfoSecond("jtx", "123456"); Console.WriteLine(delegatePractise.delegatePrintInfoThree()); delegatePractise.delegatePrintInfoFour(); Console.ReadLine(); } } }

更多精彩