委托和匿名方法的使用
来源:互联网 发布:itnc530模拟软件下载 编辑:程序博客网 时间:2024/05/16 01:33
1、多委托
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MulticastDelegateInvocationList{ public delegate int MyDelegate(int value); public class MyClass { public int Func1(int argument) { Console.WriteLine("Func1: i={0}", argument); return argument; } public int Func2(int argument) { argument *= 2; Console.WriteLine("Func2: i={0}", argument); return argument; } } class Program { static void Main(string[] args) { MyClass obj = new MyClass(); MyDelegate del1 = new MyDelegate(obj.Func1); del1 += new MyDelegate(obj.Func2); //上面两句可以简写为以下形式: //MyDelegate del1 = obj.Func1; //del1 += obj.Func2; Delegate[] ds; ds = del1.GetInvocationList(); Console.WriteLine("del1的委托调用列表中包含{0}个方法", ds.GetLength(0)); //开始执行委托 del1(5); //先调用obj1.Func1(),再调用obj1.Func2() MyDelegate del2 = new MyDelegate(obj.Func1); del2 += new MyDelegate(obj.Func2); //组合委托 Delegate mul = del1 + del2; ds = mul.GetInvocationList(); Console.WriteLine("mul的委托调用列表中包含{0}个方法", ds.GetLength(0)); int ret = (mul as MyDelegate)(10); //获取委托调用列表最后一个方法的返回值 Console.WriteLine("ret = {0}", ret); Console.ReadKey(); } }}
2、委托和匿名方法
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace UseAnonymousMethod{ //定义一个委托 public delegate int AddDelegate(int i, int j); class Program { //委托可以将方法当做参数传递. //使用委托类型的参数 public static void invokeDelegate(AddDelegate del, int i, int j) { Console.WriteLine(del(i, j)); } static void Main(string[] args) { //利用C#匿名方法特性直接给委托变量赋值 AddDelegate del = delegate(int i, int j) { return i + j; }; //通过委托变量调用匿名方法 Console.WriteLine(del(100, 200)); //直接将匿名方法作为函数参数 invokeDelegate(delegate(int i, int j) { return i + j; }, 100, 200); Console.ReadKey(); } }}
3、泛型委托
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace UseGenericDelegate{ //定义泛型委托 public delegate T MyGenericDelegate<T>(T obj); class Program { static void Main(string[] args) { //使用匿名方法给泛型委托变量赋值 MyGenericDelegate<int> del = delegate(int vlaue) { return vlaue * 2; }; //调用泛型委托变量引用的匿名方法 Console.WriteLine(del(100)); Console.ReadKey(); } }}
4、使用TimerCallBack委托实现定时回调
using System;using System.Collections.Generic;using System.Text;using System.Threading;namespace UseTimerCallback{ //用于向回调函数提供参数信息 class TaskInfo { public int count = 0; } class Program { static void Main(string[] args) { System.Console.WriteLine("敲任意键结束……"); TaskInfo ti=new TaskInfo() ; //创建Timer对象,将一个回调函数传给它,每隔一秒调用一次 Timer tm = new Timer(ShowTime, ti, 0, 1000); System.Console.ReadKey(); tm.Dispose(); } //被回调的函数 static void ShowTime(Object ti) { TaskInfo obj = ti as TaskInfo; obj.count++; System.Console.WriteLine("({0}){1}", obj.count, DateTime.Now); } }}委托可用于引用一个或多个方法,因此,多用于需要动态调用不同方法的场合
- 委托和匿名方法的使用
- 使用匿名方法 委托
- C#委托-使用匿名方法
- 委托的由来以及委托链和委托的匿名方法
- C#的委托,匿名方法和Lambda表达式
- 委托的简化语法,聊聊匿名方法和闭包
- 委托和匿名委托的比较
- 委托和匿名委托的比较
- 在线程间调用中使用匿名方法和匿名委托
- c# 多播委托 和匿名方法
- 委托和匿名方法、lambda表达式
- 【C#】Func委托类型和匿名方法
- 委托中的匿名方法和lambda表达式
- C#基础精华07(委托事件,委托的使用,匿名方法)
- 委托、匿名方法、Lambda表达式的演进
- 委托、匿名方法、Lambda表达式的演进
- 构造方法和匿名对象的使用
- 抽象类和匿名方法的使用
- 各大Asp木马的万能密码
- 放大镜
- 《第五周实验报告任务2》
- 支持向量机(SVM)、支持向量回归(SVR)
- [SIR数据集实验][2]Java类数据集相应工具使用的小经验
- 委托和匿名方法的使用
- Jmeter测试restful的webservice
- 生词
- JAVA学习之抽象类和接口
- 类型转换static_cast dynamic_cast const_cast reinterpret_cast
- 道听途说:struts、hibernate、spring简单认识
- 妖怪与和尚过河问题(java语言)
- Windows 2008 部署服务之客户端安装
- 加速ocv face dete