(第三季)委托(201~204)207 多播委托 208 匿名方法
2016-04-18 09:07
357 查看
什么是委托?
如果我们要把方法当做参数来传递的话,就要用到委托。简单来说委托是一个类型,这个类型可以赋值一个方法的引用。
声明委托
在C#中使用一个类分两个阶段,首选定义这个类,告诉编译器这个类由什么字段和方法组成的,然后使用这个类实例化对象。在我们使用委托的时候,也需要经过这两个阶段,首先定义委托,告诉编译器我们这个委托可以指向哪些类型的方法,然后,创建该委托的实例。
定义委托的语法如下:
delegate void IntMethodInvoker(int x);
定义了一个委托叫做IntMethodInvoker,这个委托可以指向什么类型的方法呢?
这个方法要带有一个int类型的参数,并且方法的返回值是void的。
定义一个委托要定义方法的参数和返回值,使用关键字delegate定义。
定义委托的其他案例:
delegate double TwoLongOp(long first,longsecond);
delegate string GetAString();
使用委托
privatedelegate string GetAString();
staticvoid Main(){
int x = 40;
GetAString firstStringMethod = newGetAString(x.ToString);
Console.WriteLine(firstStringMethod());
}
在这里我们首先使用GetAString委托声明了一个类型叫做fristStringMethod,接下来使用new对它进行初始化,使它引用到x中的ToString方法上,这样firstStringMethod就相当于x.ToString,我们通过firstStringMethod()执行方法就相当于x.ToString()
通过委托示例调用方法有两种方式
fristStringMethod();
firstStringMethod.Invoke();
委托的赋值
GetAStringfirstStringMethod = new GetAString(x.ToString);只需要把方法名给一个委托的构造方法就可以了
GetAStringfirstStringMethod = x.ToString;也可以把方法名直接给委托的实例
简单委托示例
Action委托和Func委托
除了我们自己定义的委托之外,系统还给我们提供过来一个内置的委托类型,Action和Func
Action委托引用了一个void返回类型的方法,T表示方法参数,先看Action委托有哪些
Action
Action<inT>
Action<inT1,in T2>
Action<inT1,in T2 .... inT16>最多有16种类型哦
Func引用了一个带有一个返回值的方法,它可以传递0或者多到16个参数类型,和一个返回类型
Func<outTResult>
Func<inT,out TResult>
Func<intT1,inT2,,,,,,in T16,out TResult>
多播委托
前面使用的委托都只包含一个方法的调用,但是委托也可以包含多个方法,这种委托叫做多播委托。使用多播委托就可以按照顺序调用多个方法,多播委托只能得到调用的最后一个方法的结果,一般我们把多播委托的返回类型声明为void。
Action action1 = Test1;
action2+=Test2;
action2-=Test1;
多播委托包含一个逐个调用的委托集合,如果通过委托调用的其中一个方法抛出异常,整个迭代就会停止。
取得多播委托中所有方法的委托
Actiona1 = Method1;
a1+=Method2;
Delegate[]delegates=a1.GetInvocationList();
foreach(delegated in delegates){
//d();
d.DynamicInvoke(null);
}
遍历多播委托中所有的委托,然后单独调用
匿名方法
到目前为止,使用委托,都是先定义一个方法,然后把方法给委托的实例。但还有另外一种使用委托的方式,不用去定义一个方法,应该说是使用匿名方法(方法没有名字)。
Func<int,int,int>plus = delegate (int a,int b){
int temp = a+b;
return temp;
};
intres = plus(34,34);
Console.WriteLine(res);
在这里相当于直接把要引用的方法直接写在了后面,优点是减少了要编写的代码,减少代码的复杂性
如果我们要把方法当做参数来传递的话,就要用到委托。简单来说委托是一个类型,这个类型可以赋值一个方法的引用。
声明委托
在C#中使用一个类分两个阶段,首选定义这个类,告诉编译器这个类由什么字段和方法组成的,然后使用这个类实例化对象。在我们使用委托的时候,也需要经过这两个阶段,首先定义委托,告诉编译器我们这个委托可以指向哪些类型的方法,然后,创建该委托的实例。
定义委托的语法如下:
delegate void IntMethodInvoker(int x);
定义了一个委托叫做IntMethodInvoker,这个委托可以指向什么类型的方法呢?
这个方法要带有一个int类型的参数,并且方法的返回值是void的。
定义一个委托要定义方法的参数和返回值,使用关键字delegate定义。
定义委托的其他案例:
delegate double TwoLongOp(long first,longsecond);
delegate string GetAString();
使用委托
privatedelegate string GetAString();
staticvoid Main(){
int x = 40;
GetAString firstStringMethod = newGetAString(x.ToString);
Console.WriteLine(firstStringMethod());
}
在这里我们首先使用GetAString委托声明了一个类型叫做fristStringMethod,接下来使用new对它进行初始化,使它引用到x中的ToString方法上,这样firstStringMethod就相当于x.ToString,我们通过firstStringMethod()执行方法就相当于x.ToString()
通过委托示例调用方法有两种方式
fristStringMethod();
firstStringMethod.Invoke();
委托的赋值
GetAStringfirstStringMethod = new GetAString(x.ToString);只需要把方法名给一个委托的构造方法就可以了
GetAStringfirstStringMethod = x.ToString;也可以把方法名直接给委托的实例
简单委托示例
定义一个类MathsOperations里面有两个静态方法,使用委托调用该方法 class MathOperations{ public static double MultiplyByTwo(double value){ return value*2; } public static double Square(double value){ return value*value; } } delegate double DoubleOp(double x); static void Main(){ DoubleOp[] operations={ MathOperations.MultiplyByTwo,MathOperations.Square }; for(int i =0;i<operations.Length;i++){ Console.WriteLine("Using operations "+i); ProcessAndDisplayNumber( operations[i],2.0 ); } } static void ProcessAndDisplayNumber(DoubleOp action,double value){ double res = action(value); Console.Writeline("Value :"+value+" Result:"+res); }
Action委托和Func委托
除了我们自己定义的委托之外,系统还给我们提供过来一个内置的委托类型,Action和Func
Action委托引用了一个void返回类型的方法,T表示方法参数,先看Action委托有哪些
Action
Action<inT>
Action<inT1,in T2>
Action<inT1,in T2 .... inT16>最多有16种类型哦
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace taotao { class Program { static void Main(string[] args) { Action a = PrintString; Action<string> b = PrintString; Action<int, int> c = PrintDouble; } static void PrintDouble(int i, int j) { } static void PrintInt(int i) { } static void PrintString(string str) { } static void PrintString() { Console.WriteLine("Hello world!"); // 系统内置(预定义)的一个委托类型,它可以 //指向一个没有返回值,没有参数的方法 //Action<int> a = PrintInt; } } }
Func引用了一个带有一个返回值的方法,它可以传递0或者多到16个参数类型,和一个返回类型
Func<outTResult>
Func<inT,out TResult>
Func<intT1,inT2,,,,,,in T16,out TResult>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace taotao { class Program { static void Main(string[] args) { Func<int> a = Test1; Console.WriteLine(a()); // Func<string, int> b = Test2; // func 最后一个类型是返回值类型,前面是参数类型(最多16个) int j = b("sdfdsf"); Console.ReadKey(); } static int Test1() { return 1; } static int Test2(string str) { Console.WriteLine(str); return 2; } } }
多播委托
前面使用的委托都只包含一个方法的调用,但是委托也可以包含多个方法,这种委托叫做多播委托。使用多播委托就可以按照顺序调用多个方法,多播委托只能得到调用的最后一个方法的结果,一般我们把多播委托的返回类型声明为void。
Action action1 = Test1;
action2+=Test2;
action2-=Test1;
多播委托包含一个逐个调用的委托集合,如果通过委托调用的其中一个方法抛出异常,整个迭代就会停止。
取得多播委托中所有方法的委托
Actiona1 = Method1;
a1+=Method2;
Delegate[]delegates=a1.GetInvocationList();
foreach(delegated in delegates){
//d();
d.DynamicInvoke(null);
}
遍历多播委托中所有的委托,然后单独调用
/* 猫 叫 老鼠跑,主人醒 */ using System; namespace CatCry { class MainClass { static void Test1() { Console.WriteLine("Test1"); //throw new Exception(); } static void Test2() { Console.WriteLine("Test2"); } static void Main() { // 多播委托 Action a = Test1; a += Test2; // 表示添加一个委托的引用 // // a -= Test1; //a -= Test2; // 当一个委托没有指向任何方法的时候,调用的话会出现异常。 //if (a != null) // a(); // Delegate[] delegates = a.GetInvocationList(); foreach (var item in delegates) { item.DynamicInvoke(); } Console.ReadKey(); } } }
匿名方法
到目前为止,使用委托,都是先定义一个方法,然后把方法给委托的实例。但还有另外一种使用委托的方式,不用去定义一个方法,应该说是使用匿名方法(方法没有名字)。
Func<int,int,int>plus = delegate (int a,int b){
int temp = a+b;
return temp;
};
intres = plus(34,34);
Console.WriteLine(res);
在这里相当于直接把要引用的方法直接写在了后面,优点是减少了要编写的代码,减少代码的复杂性
/* 猫 叫 老鼠跑,主人醒 */ using System; namespace CatCry { class MainClass { static int Test1(int arg1, int arg2) { return arg1 + arg2; } static void Main() { //Func<int, int, int> plus = Test1; // 修改成: 匿名方法 的形式 Func<int, int, int> plus = delegate (int arg1, int arg2) { return arg2 + arg1; }; // 匿名方法本质上是一个方法,只是没有名字,任何使用委托变量的地方都可以使用匿名方法赋值 Console.ReadKey(); } } }
相关文章推荐
- new image() 和 Img 加载
- ubuntu emacs
- C语言之基本算法24—黄金切割法求方程近似根
- 验证码识别总结
- CubeMX中的HAL库之IWDG配置篇
- CSS3是怎么实现全景特效?
- window安装MongoDB并配置复制集
- .c文件如何编译为ko的MAKEFILE文件编写
- MySql 笔记
- Objective-C Runtime之着魔的UIAlertView
- C++学习笔记之三 数据处理(基本数据类型)
- 知识点1-2
- Python读写CSV数据
- 机器学习实战之回归
- Swift (一)
- 搜索栏 UISearchController
- 调用系统相册
- 网络安全
- 多线程
- 网络请求(NSURLSession)