网站推广公司认准乐云seo,second是什么意思,建设厅焊工证查询网站,模板网站如何建站什么是委托
可以认为委托是持有一个或多个方法的对象。但它与对象不同#xff0c;因为委托可以被执行。当执行委托时#xff0c;委托会执行它所“持有”的方法。先看一个完整的使用示例。
// See https://aka.ms/new-console-template for more informationdelegate void M…什么是委托
可以认为委托是持有一个或多个方法的对象。但它与对象不同因为委托可以被执行。当执行委托时委托会执行它所“持有”的方法。先看一个完整的使用示例。
// See https://aka.ms/new-console-template for more informationdelegate void MyDel(int value); // 声明委托类型class Program
{void PrintLow(int value){Console.WriteLine({0} - Low value, value);}void PrintHigh(int value){Console.WriteLine({0} - High value, value);}static void Main(string[] args){Program p new Program();MyDel del; // 声明委托变量Random rand new Random();int randomValue rand.Next(1, 101);del randomValue 50 ? p.PrintLow : p.PrintHigh; // 选择调用哪个方法del(randomValue); // 调用委托方法}
}
如果生成的随机数 randomValue 小于50则 del 引用的是 p.PrintLow否则del 引用的是 p.PrintHigh。
委托概述
下图是使用类和委托的对比
可以把 delegate 看成一个包含有序方法列表的对象这些方法具有相同的签名和返回类型。如下图所示:
委托的几点说明 方法的列表称为调用列表 委托保存的方法可以来自任何类或者结构只要它们在委托的返回类型和委托的签名包括ref和out修饰符上保持一致 调用列表中的方法可以是实例方法和静态方法 执行委托时会按照方法的添加顺序执行调用列表。
声明委托类型 创建委托对象
委托是引用类型因此初始化委托变量需要创建一个对象。有两种方式创建委托对象 使用 new 运算符 class Obj
{void MyM1(int value) {}static void Other(int value) {}}
delegate void MyDel(int value);MyDel del1, del2;Obj obj new Obj();
del1 new MyDel(obj.MyM1); // 使用一个对象的方法
del2 new MyDel(Obj.Other); // 使用静态方法创建委托对象省略 new 运算符 MyDel del1, del2;
del1 obj.MyM1; // 使用一个对象的方法
del2 Obj.Other; // 使用静态方法创建委托对象当为委托变量赋值时除了为委托分配内存创建委托对象还会把第一个方法放入委托的调用列表。 当然我们还可以在声明委托变量时初始化委托变量。 MyDel del1 obj.MyM1;当我们为同一个委托变量赋值另外一个委托对象时之前的委托对象就会被垃圾回收器回收。 MyDel delVar;
delVar myInstObj.MyM1;
...
delVar SClass.OtherM2;组合委托
可以将两个同类型的委托变量进行 操作赋值给另外一个同类型的新变量完成委托的组合
MyDel delA myInstObj.MyM1;
MyDel delB SClass.OtherM2;MyDel delC delA delB;为委托添加或者删除方法
委托可以持有多个方法 通过使用运算符 和 - 可以为委托添加或者删除方法。
MyDel del inst.MyM1;
del SCl.m3;
del X.Act;从委托移除方法
del - SCl.m3;移除委托时需要注意以下事项
如果委托的调用列表中存在多个实例- 运算符将从列表的最后开始搜索并移除第一个与方法匹配的实例当要删除的方法在调用列表不存在时什么也不会发生试图调用空委托将会导致异常因此执行委托之前有必要进行 null 判空。
调用委托
调用委托的方式与调用方法一样。用于调用委托的参数将会传递给调用列表当中的每一个方法除非有输出参数。
MyDel delVar inst.MyM1;
delVar SCl.m3;
delVar X.Act;
...
delVar(55);下面是一个完整的示例
delegate void PrintFunction();class Test
{public void Print1(){Console.WriteLine(Print1 -- instance method);}public static void Print2(){Console.WriteLine(Print2 -- static method);}}class Program
{static void Main(){Test t new Test();PrintFunction pf;
// 实例方法pf t.Print1;// 给委托增加3个另外的方法pf Test.Print2;pf t.Print1;pf Test.Print2; // 现在委托含有4个方法if (null ! pf){pf();}else{Console.WriteLine(委托为空);}}
}输出如下
Print1 -- instance method
Print2 -- static method
Print1 -- instance method
Print2 -- static method调用带引用参数的委托
如果委托有引用参数在调用委托列表中的下一个方法时参数的新值会传给下一个方法。
delegate void MyDel(ref int x);class MyClass
{public void Add2(ref int x) { x 2; }public void Add3(ref int x) { x 3; }static void Main(){MyClass mc new MyClass();MyDel del mc.Add2;del mc.Add3;del mc.Add2;int x 5;del(ref x);Console.WriteLine(Value: {0}, x);}
}输出如下
Value: 12Lambda 表达式
C# 当中的 Lambda 表达式是一种简洁的方式来表示匿名方法。通常用于简化代码尤其是在与 LINQ、委托或事件等相关的场景中。LINQ 和 事件会在后面相关文章中讲到。
语法
(parameters) expressionparameters代表输入参数。如果只有一个参数可以省略()。称为 lambda 操作符表示从参数到表达式或代码块的映射。expression返回的表达式。如果有多条语句可以使用代码块{}。
Lambda 表达式的常见使用场景
委托与 Lambda 表达式
delegate void MyDel(int x);MyDel del x x *2;用于 FuncT委托
using System;class Program
{static void Main(){// Func 委托接受两个整数参数返回它们的和Funcint, int, int add (a, b) a b;int result add(3, 4);Console.WriteLine(result); // 输出 7}
}Func 是 C# 中一个常用的泛型委托类型在 C# 3.0 中引入专门用于表示带有返回值的方法。在使用时FuncT 委托的最后一个泛型参数是返回值类型必须有前面的泛型参数是输入参数类型可以没有输入参数。 Lambda 表达式与 LINQ 查询 using System;
using System.Linq;
using System.Collections.Generic;class Program
{static void Main(){Listint numbers new Listint { 1, 2, 3, 4, 5, 6 };// 使用 Lambda 表达式筛选出大于 3 的数var result numbers.Where(n n 3).ToList();foreach (var number in result){Console.WriteLine(number); // 输出 4, 5, 6}}
}用于事件处理 using System;class Program
{static void Main(){Actionstring messagePrinter msg Console.WriteLine(msg);messagePrinter(Hello, Lambda!); // 输出 Hello, Lambda!}
}
委托的使用场景
委托的主要使用场景如下
事件处理。回调函数。
以上两个使用场景的具体例子会在后面文章介绍到。
小结本章主要介绍了 C# 当中委托和 Lambda 的概念、用法。
各位道友码字不易如有收获记得一键三连啊。 文章转载自: http://www.morning.pxlsh.cn.gov.cn.pxlsh.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.gghhmi.cn.gov.cn.gghhmi.cn http://www.morning.routalr.cn.gov.cn.routalr.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.ttdxn.cn.gov.cn.ttdxn.cn http://www.morning.swwpl.cn.gov.cn.swwpl.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.080203.cn.gov.cn.080203.cn http://www.morning.xhlht.cn.gov.cn.xhlht.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.wanjia-sd.com.gov.cn.wanjia-sd.com http://www.morning.mmynk.cn.gov.cn.mmynk.cn http://www.morning.dbrnl.cn.gov.cn.dbrnl.cn http://www.morning.yrbqy.cn.gov.cn.yrbqy.cn http://www.morning.cxsdl.cn.gov.cn.cxsdl.cn http://www.morning.qdrrh.cn.gov.cn.qdrrh.cn http://www.morning.hkswt.cn.gov.cn.hkswt.cn http://www.morning.clccg.cn.gov.cn.clccg.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.thmlt.cn.gov.cn.thmlt.cn http://www.morning.lnbcg.cn.gov.cn.lnbcg.cn http://www.morning.qlpq.cn.gov.cn.qlpq.cn http://www.morning.bkgfp.cn.gov.cn.bkgfp.cn http://www.morning.dhbyj.cn.gov.cn.dhbyj.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.gdpai.com.cn.gov.cn.gdpai.com.cn http://www.morning.kynf.cn.gov.cn.kynf.cn http://www.morning.tralution.cn.gov.cn.tralution.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.gbsby.cn.gov.cn.gbsby.cn http://www.morning.ylsxk.cn.gov.cn.ylsxk.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.hgfxg.cn.gov.cn.hgfxg.cn http://www.morning.fhhry.cn.gov.cn.fhhry.cn http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn http://www.morning.rdbj.cn.gov.cn.rdbj.cn http://www.morning.kuaijili.cn.gov.cn.kuaijili.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.jjwt.cn.gov.cn.jjwt.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.msgnx.cn.gov.cn.msgnx.cn http://www.morning.xckqs.cn.gov.cn.xckqs.cn http://www.morning.tzcr.cn.gov.cn.tzcr.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.plqsz.cn.gov.cn.plqsz.cn http://www.morning.hhzdj.cn.gov.cn.hhzdj.cn http://www.morning.mztyh.cn.gov.cn.mztyh.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.ngcth.cn.gov.cn.ngcth.cn http://www.morning.mzskr.cn.gov.cn.mzskr.cn http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.wzknt.cn.gov.cn.wzknt.cn http://www.morning.hjlwt.cn.gov.cn.hjlwt.cn http://www.morning.tqsmg.cn.gov.cn.tqsmg.cn http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn http://www.morning.wktbz.cn.gov.cn.wktbz.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.rbjp.cn.gov.cn.rbjp.cn http://www.morning.poapal.com.gov.cn.poapal.com http://www.morning.ccjhr.cn.gov.cn.ccjhr.cn http://www.morning.tmrjb.cn.gov.cn.tmrjb.cn http://www.morning.bfjyp.cn.gov.cn.bfjyp.cn http://www.morning.gqfjb.cn.gov.cn.gqfjb.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.qgghr.cn.gov.cn.qgghr.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.ckfyp.cn.gov.cn.ckfyp.cn http://www.morning.qtyfb.cn.gov.cn.qtyfb.cn http://www.morning.ztqyj.cn.gov.cn.ztqyj.cn http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn http://www.morning.ckdgj.cn.gov.cn.ckdgj.cn http://www.morning.rgksz.cn.gov.cn.rgksz.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.zjqwr.cn.gov.cn.zjqwr.cn http://www.morning.mqfw.cn.gov.cn.mqfw.cn http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn http://www.morning.qxxj.cn.gov.cn.qxxj.cn http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn