好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

C#委托、Lambda表达式.doc

6页
  • 卖家[上传人]:hs****ma
  • 文档编号:514788118
  • 上传时间:2023-04-11
  • 文档格式:DOC
  • 文档大小:143.50KB
  • / 6 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 委托、Lambda表达式1. 委托 2(1) 委托的本质 2(2) 使用委托的示例 2(3) 委托的Invoke与BeginInvoke方法 21、 Invoke方法 22、 BeginInvoke方法 23、 委托的EndInvoke方法 3(4) Action、Func与Predicate(泛型委托) 31、 Action无返回值的泛型委托 32、 Func带有返回值的泛型委托 43、 Predicate委托(常用于集合参数) 4(5) 多播委托 4(6) 使用委托定义匿名方法 52. Lambda表达式 5(1) Lambda表达式本质 5(2) Lambda表达式使用示例 5(3) Lambda表达式语法 61、 表达式Lambda语法(参数语法) 62、 语句Lambda语法(函数体语法) 63、 类型猜测 64、 Lambda式中的变量作用域 6作者:李志伟时间:2014-01-181. 委托(1) 委托的本质委托实际上就是指向函数的指针在C#中委托是一种类型,定义一个委托就是定义一个新的类,它与类的地位是一样的,所以可以定义类的地方都可以定义委托!实际上,使用delegate关键字定义的委托继承了System.MulticastDelegate类,而System.MulticastDelegate类又继承了System.Delegate。

      Delegate:表示委托,委托是一种数据结构,它引用静态方法或引用类实例及该类的实例方法MulticastDelegate:表示多路广播委托;即,其调用列表中可以拥有多个元素的委托2) 使用委托的示例 delegate string DelegateMethod(int a, int b);//定义委托 class Program { static void Main(string[] args) { DelegateMethod dm = new DelegateMethod(Method);//或:DelegateMethod dm = Method Console.WriteLine(dm(10, 20));//执行委托 Console.Read(); } private static string Method(int a, int b) { return "相加结果:" + (a + b); } }(3) 委托的Invoke与BeginInvoke方法1、 Invoke方法实际上,给委托实例提供圆括号与调用委托的Invoke方法效果完全相同: delegate string DelegateMethod(int a, int b);//定义委托 class Program { static void Main(string[] args) { DelegateMethod dm = Method; Console.WriteLine(dm.Invoke(10, 20));//执行委托 Console.Read(); } private static string Method(int a, int b) { return "相加结果:" + (a + b); } }2、 BeginInvoke方法调用委托实例的BeginInvoke方法就是开启一个新的线程执行委托实例指向的方法。

      delegate void DelegateMethod(int a, int b);//定义委托 class Program { static void Main(string[] args) { DelegateMethod dm = Method; for (int i = 0; i < 10; i++) { dm.BeginInvoke(i, 20, null, null);//异步调用委托 } Console.Read(); } private static void Method(int a, int b) { Console.WriteLine("相加结果:" + (a + b));//执行委托 } }BeginInvoke方法有三种参数,第一种参数是委托实例指向的方法的参数(可能有多个);第二种参数的类型是AsyncCallback,AsyncCallback是一个委托类型,其定义是delegate void AsyncCallback(IAsyncResult ar),当调用BeginInvoke方法的委托实例异步执行完成时,就会执行该参数指向的方法;第三种参数是object类型,主要是向第二种参数传递一些值,一般可以传递被调用方法的委托,这个值可以使用IAsyncResult.AsyncState属性获得。

      具体请参考“C#多线程”) delegate void DelegateMethod(int a, int b);//定义委托 class Program { static void Main(string[] args) { DelegateMethod dm = Method; dm.BeginInvoke(10, 20, MethodCompleted, null);//异步调用委托 Console.Read(); } //异步委托 private static void Method(int a, int b) { Console.WriteLine("相加结果:" + (a + b)); Thread.Sleep(3000); } //回调函数 private static void MethodCompleted(IAsyncResult ar) { Console.WriteLine("休眠结束!"); } }3、 委托的EndInvoke方法如果调用了委托实例的BeginInvoke方法,就可以通过EndInvoke方法获得委托实例指向的方法的返回值,或是确定指向的方法已经被成功调用。

      具体请参考“C#多线程”) delegate string DelegateMethod(int a, int b);//定义委托 class Program { static void Main(string[] args) { DelegateMethod dm = Method; IAsyncResult ar = dm.BeginInvoke(10, 20, null, null);//异步调用委托 string result = dm.EndInvoke(ar);//等待委托异步调用结束 Console.WriteLine(result);//输出返回值 Console.Read(); } //异步委托 private static string Method(int a, int b) { Thread.Sleep(3000); return "相加结果:" + (a + b); } }(4) Action、Func与Predicate(泛型委托)除了为每个参数和返回类型定义一个新委托之外,还可以使用Action与Func泛型委托。

      使用泛型委托主要的优势是可以省略委托的定义1、 Action无返回值的泛型委托泛型Action委托表示引用一个void返回类型的方法,这个委托类可以有多个(≥0)参数且参数的类型可以不同(最多可以有16种不同类型的参数) class Program { static void Main(string[] args) { Action dm = Method;//委托实例 dm(10,20);//调用委托 Console.Read(); } private static void Method(int a, int b) { Console.WriteLine("相加结果:" + (a + b)); } }2、 Func带有返回值的泛型委托泛型Func委托与Action委托类似,不同点是Func允许调用带返回值类型的方法,而且其返回值类型的指定是Func中的最后一个类型(Tn),即Tn就是其返回值,其它类型都表示参数的类型。

      最多可以有16种参数类型和一个返回值类型) class Program { static void Main(string[] args) { Func dm = Method;//委托实例 string result = dm(10, 20);//调用委托 Console.WriteLine(result);//输出返回值 Console.Read(); } private static string Method(int a, int b) { return "相加结果:" + (a + b); } }3、 Predicate委托(常用于集合参数)Predicate泛型委托,只能接受一个传入参数,返回值为bool类型 class Program。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.