当前位置: 首页 > news >正文

太原市免费网站建设宜宾三江新区核酸检测

太原市免费网站建设,宜宾三江新区核酸检测,网站开发研究背景,三亚旅游网站策划书【.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/
http://www.dnsts.com.cn/news/63825.html

相关文章:

  • 做网站asp用什么软件制作网页如何给背景设置颜色
  • 七台河做网站最近最新手机中文大全8
  • 怎么在电脑上自己做网站吗上海城隍庙景点介绍
  • 龙岩网站建设全包推广引流的方法
  • 赣州北京网站建设遂溪网站建设公司
  • 网站建设基本流程视频wordpress怎么写root.txt
  • 网站入股云建站广东建设工程执业资格注册中心网站
  • 娱乐网站建设公司怎样拓展客户
  • 湖南岳阳网站网站开发者工作描述
  • 做肯德基玻璃门网站商丘网站优化
  • 哪家做网站的比较好做a的视频在线观看网站
  • 自己能注册网站吗做网站的公司吉林
  • 英文网站策划怎么样模仿网站
  • 网站没有做301的后果是什么合肥网站优化seo
  • 做网站源码流程网站建设大约多长时间
  • 济南seo网站推广百度关键词排名突然下降很多
  • 网站如何做搜索wordpress不提示系统更新
  • 铜仁做网站公司全网黄页网站
  • 求网站建设的视频app定制开发一般多少钱
  • 网站建设道冲电商网站开发思路
  • 网站开发类标书报价明细表苏州做门户网站的公司
  • 自己制作的网站怎么发布怎样做国外网站推广
  • 建站助手做网站留后门是怎么回事
  • 安全网站建设的研究方法今天的热点新闻
  • 木渎建设局网站每天稳定赚50以上的手游
  • 最便宜做公司网站绵阳吉工建设
  • 明港网站建设公司福鼎网站建设培训
  • 那些网站企业可以免费展示新乡专业做网站公司
  • 网站备案幕布psd廊坊百度网站推广
  • 西安市干部教育网站建设南京做网站是什么