整理几种委托的用法Action,Func,匿名,多播,Lambde表达式,event
2017-05-08 20:37
531 查看
1.常用委托形式:
例子:
3.带参数的委托
6.多播委托
7.匿名委托
8.Lambda表达式
9.event
例子:
public static void Main (string[] args) { Meet ("China"); Meet ("Japan"); } public static void SayHelloInChina() { Console.WriteLine ("吃了吗?"); } public static void SayHelloInAmerica() { Console.WriteLine ("what is the weather?"); } public static void SayHelloInJapan() { Console.WriteLine ("孔丽西瓦!"); } public static void SayHelloInHanGuo() { Console.WriteLine ("……思密达"); } public static void SayHelloInAnyCountry(){} public static void Meet(string s) { if (s == "China") { SayHelloInChina (); } if (s == "America" ) { SayHelloInAmerica (); } if (s == "Japan") { SayHelloInJapan (); } if (s == "HanGuo") { SayHelloInHanGuo (); } }应用委托:
public static void Main (string[] args) { //通过委托去执行方法 //委托的本质就是将方法作为参数进行引用 Meet(SayHelloInChina); //直接把需要执行的方法名字传进来,委托会自动匹配 Meet (SayHelloInHanGuo); } public static void SayHelloInChina() { Console.WriteLine ("吃了吗?"); } public static void SayHelloInAmerica() { Console.WriteLine ("what is the weather?"); } public static void SayHelloInJapan() { Console.WriteLine ("孔丽西瓦!"); } public static void SayHelloInHanGuo() { Console.WriteLine ("……思密达"); } public static void SayHelloInAnyCountry(){} public static void Meet (SayHello say)//把委托传进来 { say (); //去执行这个委托 }2.委托形式二
public delegate void kid(); //声明一个小弟的委托 class MainClass { public static void Main (string[] args) { //实例化大哥类 Boss boss = new Boss (); //给委托添加委托的方法 kid kid = new kid (boss.Robe); kid += boss.Kill; //委托链, kid += boss.Fire; //消除委托的方法 kid -= boss.Kill; kid -= boss.Fire; kid -= boss.Robe; //一般委托会这么用,不然委托里面委托的事件为空的话,会出现空引用的警告 if (kid != null) { kid (); //执行委托 } } } //定义大哥类 public class Boss { public void Fire(){ Console.WriteLine ("放火……"); } public void Kill() { Console.WriteLine ("砍他……"); } public void Robe() { Console.WriteLine ("抢劫了,把钱交粗来……"); } }
3.带参数的委托
/// <summary> /// 计算器的委托 /// </summary> public delegate double DlgtCounter (float a,float b); class MainClass { public static void Main (string[] args) { Count ct = new Count (); // DlgtCounter dcer = new DlgtCounter(ct.Add); Accountant ac = new Accountant (); // Console.WriteLine (ac.Operation (dcer, 4f, 5f)); Console.WriteLine (ac.Operation (ct.Add, 3f, 5f)); Console.WriteLine (ac.Operation (ct.Sub, 3f, 5f)); Console.WriteLine (ac.Operation (ct.Mul, 3f, 5f)); Console.WriteLine (ac.Operation (ct.Divide, 3f, 5f)); } } /// <summary> /// 会计类,里面有操作的方法 /// </summary> public class Accountant { //利用计算器(委托)去计算 public double Operation(DlgtCounter counter,float a, float b) { return counter (a, b); } } /// <summary> /// 计算类,里面有加减乘除的方fgfcr0法 /// </summary> public class Count { public double Add(float a,float b) { return a + b; } public double Sub(float a,float b) { return a - b; } public double Mul(float a,float b) { return a * b; } public double Divide(float a,float b) { return a / b; } }4.Action
public static void Main (string[] args) { //Action可以通过泛型去指定Action指向的方法的多个参数类型 //参数的类型必须要与Action声明时后面的参数一致(顺序也不能错) Action a = PrintHelloWorld; a (); Action<string> b = PrintString; b ("shenmoyisi"); Action<int,int> c = PrintDoubleInt; c (2, 4); Action<int,int,string, bool> d = Print; d (1, 2, "干什么", false); } public static void PrintHelloWorld() { Console.WriteLine ("Hello World."); } public static void PrintString(string str) { Console.WriteLine (str); } public static void PrintDoubleInt(int a, int b) { Console.WriteLine (a+ " "+ b); } public static void Print(int a, int b, string c, bool d) { Console.WriteLine (a+ " " + b+ " " +c+ " " + d); }5.Func
//Func 代表具有返回值的委托, //Func中可以跟很多类型,<>中前面的都是参数类型,最后一个是返回值类型 //参数类型必须跟指定方法的参数一致,有0~16个参数 class MainClass { public static void Main (string[] args) { Func<int> a = Test01; Console.WriteLine (a ()); Func<string , int> b = Test02; Console.WriteLine (b ("干什么")); Func<int,int,int> c = Test03; int res = c (12,45); Console.WriteLine (res); } public static int Test01(){ return 1; } public static int Test02(string str) { return 2; } public static int Test03(int a,int b) { return a + b; } }
6.多播委托
class MainClass { public static void Main (string[] args) { Action a = Test01; a += Test02; // a -= Test01; //消除方法 // a -= Test02; if (a != null) { a (); } //当一个委托,没有指向任何一个方法的时候调用会出现空引用异常 //如果方法有返回值,那么多播委托(委托链)只能得到最后一个方法的返回值 //一般我们会把多播委托,的返回值定义成void //多播委托是一个逐个调用的委托集合 //如果通过委托调用的任何一个方法出现的异常,整个迭代就会停止。 //定义一个委托集合。 得到多播委托里面所有的单个委托 Delegate[] dlgts = a.GetInvocationList(); //遍历所有委托 foreach (Delegate i in dlgts) { //执行委托 i.DynamicInvoke (); } } public static void Test01() { Console.WriteLine ("Test01"); // throw new Exception (); //抛出一个异常,执行不了,也就是说,委托连,中有一个异常就会执行不了 } public static void Test02() { Console.WriteLine ("Test02"); } }
7.匿名委托
class MainClass { public static int Plus(int a,int b) { return a + b; } public static void Main (string[] args) { //不使用匿名方法 Func<int,int,int> f1 = Plus; int res1 = f1(12, 34); Console.WriteLine ("通过f1得到: "+res1); //使用匿名方法,本质上是一个方法,只是没有名字,也不需要写返回值 //任何使用委托方法的地方都能使用匿名方法进行赋值 Func<int,int,int> f2 = delegate (int a, int b) { return a + b; }; int res2 = f2 (23, 45); Console.WriteLine ("通过f2得到: " +res2); Func<int,int,int> f3 = delegate(int a, int b) { return a - b; }; int qwe = f3(4,2); } }
8.Lambda表达式
public static void Main (string[] args) { //匿名方法 Func<int,int,int> f2 = delegate (int a, int b) { return a + b; }; //Lambda表达式,用来代替匿名方法,所以表达式中也定义一个方法 //Lambda表达式中的参数是可以不声明类型的 Func<int,int,int> f3 = (arg1, arg2) => { return arg1 + arg2; }; // Func<int,int,int>f3 = (arg1, arg2) => arg1 + arg2; Console.WriteLine (f3(12,12)); //Lambda表达式中只有一个参数的时候可以不加小括号 //当函数的语句只有一句的时候,可以不加大括号和return; //如果说多条语句的会必须加 Func<int,int>f4 = arg => 456; Console.WriteLine ("f4: " + f4(123)); //任何使用委托方法的地方都能使用匿名方法进行赋值 //只要有匿名方法的地方就能使用Lambda表达式 //Lambda表达式,可以用来代替匿名方法 Func<int,int> f5 = arg => ++arg; Console.WriteLine ("f5 : " + f5(13)); Func <string,int> f6 = asdf => 1234; int azxc = f6 ("干什么"); } }
9.event
public delegate void MyDelegate(); class MainClass { //声明一个委托类型的变量 // public MyDelegate myDelegate; //声明一个事件,该怎么用怎么用,没有什么改变, public event MyDelegate myDelegate; public static void Main (string[] args) { MainClass mc = new MainClass (); mc.myDelegate = Test; mc.myDelegate (); } public static void Test() { Console.WriteLine ("Test"); } } public delegate void MyDelegete(); class Test01 { public event MyDelegate MyDelegete01; public static void Main01(string[] args01){ Test01 t01 = new Test01 (); t01.MyDelegete01 = Test02; t01.MyDelegete01 (); } public static void Test02() { Console.WriteLine ("干什么"); } }
相关文章推荐
- 为LINQ服务的C#新特性总结篇---扩展方法,匿名委托,lambda表达式,Action委托,Func委托,Linq中的order by,top和sum函数
- Lambda 表达式创建委托Action和Func时的简单使用
- Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)
- Func与Action, delegate, event, var, dynamic, 匿名方法,lambda, 大量的关键都使用相同功能,大大增加C#复杂性
- 通过Func 委托理解委托和匿名方法及Lambda 表达式
- 温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件
- [ZZ]使用Action、Func,EventHandler,params关键字修饰的参数的匿名委托和Lambda表达式
- Delegate,Action,Func,匿名方法,匿名委托,事件
- Delegate,Action,Func,匿名方法,匿名委托,事件
- Delegate,Action,Func,匿名方法,匿名委托,事件
- 温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件
- 温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件
- Delegate,Action,Func,匿名方法,匿名委托,事件
- C#基础:委托之Action<T>和Func<T>的用法
- Delegate,Action,Func,匿名方法,匿名委托,事件
- Action与Func 用法 以及和委托的关系
- Delegate,Action,Func,匿名方法,匿名委托,事件
- C#基础:委托之Action<T>和Func<T>的用法(转)
- 温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件
- 匿名方法、Lambda表达式、Func<>委托