太原市免费网站建设,宜宾三江新区核酸检测,网站开发研究背景,三亚旅游网站策划书【.NET Core】委托#xff08;Delegate#xff09;应用详解 文章目录 【.NET Core】委托#xff08;Delegate#xff09;应用详解一、概述二、委托#xff08;Delegate#xff09;定义三、基础委托(Delegate) - 无返回值委托四、基础委托(Delegate) - 有返回值委托五、Mu…【.NET Core】委托Delegate应用详解 文章目录 【.NET Core】委托Delegate应用详解一、概述二、委托Delegate定义三、基础委托(Delegate) - 无返回值委托四、基础委托(Delegate) - 有返回值委托五、MulticastDelegate 多播委托六、匿名方法七、匿名委托之Action八、匿名委托之Func九、委托总结十、参考资料 一、概述
委托Delegate是存有对某个方法的引用的一种引用类型变量。引用可在运行时被改变。委托Delegate类似于C或C中函数指针。委托是将方法调用者和目标方法动态关联起来委托是一个类实质上是一个类可以通过委托隐藏方法虽然委托定义方法的参数及其返回值但是它并不是和方法一个层级的。
委托Delegate可以将方法当作另一个方法的参数来进行传递这种方法动态地赋给参数的做法可以避免在程序中大量使用If-Else(Switch)语句同时使得程序具有更好的可扩展性。
二、委托Delegate定义
声明一个委托的语法如下
deletgate return type delegate-name parameter listdelegate 定义委托关键字return type 返回类型 与方法返回值相同delegate-name 委托的命名parameter list 参数列表 与方法入参相同
声明委托步骤
声明一个delegate对象它应当与要传统的方法具有相同的参数和返回值类型。创建delegate对象new Delegate传入参数通过delegate调用方法并返回值。
代码演示
delegate int DegaAdd(int a,int b,int c);
public int add(int a,int b,int c){return abc;
}
public static void Main(String[] args){DegaAdd degaAddnew DegaAdd(add);int respVal degaAdd(2,3,4);Console.WriteLine(respVal);
}三、基础委托(Delegate) - 无返回值委托
public class Program
{public delegate void DelegateNoReturn(string param1, string param2);public static void NoReturn(string param1, string param2) {Console.WriteLine(param1 param2.);}public static void Main(string[] args) {DelegateNoReturn delegateNoReturn;delegateNoReturn NoReturn;delegateNoReturn(Hello, Delegate);}
}四、基础委托(Delegate) - 有返回值委托
public class Program
{public delegate void DelegateNoReturn(string param1, string param2);public delegate string DelegateHaveReturn(string param1, string param2);public static void NoReturn(string param1, string param2) {Console.WriteLine(param1 param2.);}public static string HaveReturn(string param1, string param2) {return (有参数Delegate:param1 param2 .);}public static void Main(string[] args){DelegateHaveReturn delegateHaveReturn;delegateHaveReturn HaveReturn;string response delegateHaveReturn(Hello, Delegate);Console.WriteLine(response);}
}五、MulticastDelegate 多播委托
多播委托MulticastDelegate是指在一个委托中注册多个方法在注册方法时可以在委托中使用加号运算符或者减号运算符来实现添加或撤销方法。
创建一个方法集合类
public class DelegateMethod
{public static void Method1(){Console.WriteLine(委托方法一.);}public void Method2() {Console.WriteLine(委托方法二.);}public void Method3(){Console.WriteLine(委托方法三.);}
}创建一个多播委托
public class Program
{public delegate void OrderDelegate();public static void Main(string[] args) {OrderDelegate orderDelegate new OrderDelegate(DelegateMethod.Method1);orderDelegate new DelegateMethod().Method2;orderDelegate new DelegateMethod().Method3;orderDelegate();}
}输出
委托方法一.
委托方法二.
委托方法三.六、匿名方法
匿名方法Anonymous methods提供了一种传递代码块作为委托参数的技术。匿名方法是没有名称只有主体的方法。
在匿名方法中您不需要指定返回类型它是从方法体内的return语句推断的。
.net 3.0以前的版本匿名方法是通过使用delegate关键字创建委托实例来声明的
delegate void ValueChange(int n);
...
ValueChange vchangedelegate(int x)
{Console.WriteLine(Anonymous Method:{0},x);
}对于高于C#3.0 的版本中可以用Lambda表达式进行取代匿名方法并用Lambda表达式作为编写内联代码的首选方式因为它更简洁。
button1.Clickdelegate(Object o,EventArgs e)
{Console.WriteLine(Anonymous Method:);
}七、匿名委托之Action
Action是.NET Framework内置的泛型委托可以使用Action委托以参数形式传递方法而不用显示声明自定义的委托。封装的方法必须与此委托定义的方法签名相对应。也就是说封装的方法必须具有一个通过值传递给它的参数并并且不能有返回值。
Action的特点
参数个数0~16没有返回值
public class ActionDemo
{public void Operate() {Action action1 new Action(Method1);Actionint, int action2 new Actionint, int(Method2);action1();action2(1, 2);Actionint ,string,string action3 delegate (int i1 ,string i2,string s){Console.WriteLine($这里是三个参数的Action委托参数1的值是{i1}参数2的值是{i2}参数3的值是{s});};action3(1,a,abc);}public void Method1() {Console.WriteLine(This Method1);}public void Method2(int a,int b) { Console.WriteLine(This Method2); }}八、匿名委托之Func
Func匿名委托与Action相似但是Func委托代表有返回类型的委托。
Func至少0个输入参数至多16个输入参数根据返回值泛型返回。必须有返回值不可void
Func 表示没有输入参参返回值为int类型的委托。
Funcobject,string,int 表示传入参数为object, string 返回值为int类型的委托。
Funcobject,string,int 表示传入参数为object, string 返回值为int类型的委托。
FuncT1,T2,T3,int 表示传入参数为T1,T2,T3(泛型)返回值为int类型的委托。
public void Operate()
{// 无参数只要返回值 Funcint fun1 new Funcint(FunWithNoPara);int result1 fun1();Console.WriteLine(result1);Console.WriteLine(----------------------------);Funcint fun2 delegate { return 19; };int result2 fun2();Console.WriteLine(result2);Console.WriteLine(----------------------------);Funcint fun3 () { return 3; };int result3 fun3();Console.WriteLine(result3);Console.WriteLine(----------------------------);//有一个参数一个返回值Funcint, int fun4 new Funcint, int(FunWithPara);int result4 fun4(4);Console.WriteLine($这里是一个参数一个返回值的方法返回值是{result4});Console.WriteLine(----------------------------);// 使用委托Funcint, string fun5 delegate (int i) { return i.ToString(); };string result5 fun5(5);Console.WriteLine($这里是一个参数一个返回值的委托返回值是{result5});Console.WriteLine(----------------------------);// 使用匿名委托Funcint, string fun6 (int i) {return i.ToString();};string result6 fun6(6);Console.WriteLine($这里是一个参数一个返回值的匿名委托返回值是{result6});Console.WriteLine(----------------------------);// 多个输入参数Funcint, string, bool fun7 new Funcint, string, bool(FunWithMultiPara);bool result7 fun7(2, 2);Console.WriteLine($这里是有多个输入参数的方法返回值是{result7});Console.WriteLine(----------------------------);// 使用委托Funcint, string, bool fun8 delegate (int i, string s){return i.ToString().Equals(s) ? true : false;};bool result8 fun8(2, abc);Console.WriteLine($这里是有多个输入参数的委托返回值是{result8});Console.WriteLine(----------------------------);// 使用匿名委托Funcint, string, bool fun9 (int i, string s) {return i.ToString().Equals(s) ? true : false;};bool result9 fun9(45, ert);Console.WriteLine($这里是有多个输入参数的匿名委托返回值是{result9});Console.ReadKey();}public int FunWithNoPara(){return 10;}public int FunWithPara(int i){return i;}public bool FunWithMultiPara(int i, string s){return i.ToString().Equals(s) ? true : false;}九、委托总结
委托封装了包含特殊返回值和一组参数行为类似于单一方法接口。委托类型声明中描述的类型签名决定了方法哪个方法可用于委托实例同时也决定了调用签名。创建委托实例需要一个方法以及对于实例方法来说调用方法的目标。委托实例是不易变的。每个委托实例都包含一个调用列表——一个操作列表。委托实例可以合并到一起也可以从一个委托实例中删除另一个。
十、参考资料
https://learn.microsoft.com/zh-cn/dotnet/api/system.func-1?viewnet-7.0
https://learn.microsoft.com/zh-cn/dotnet/csharp/programming-guide/delegates/