标签:
1 namespace MyLambda 2 { 3 public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16, in T17>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, T17 arg17); 4 public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16, in T17, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16, T17 arg17); 5 public delegate void NoReturnNoParaOut(); 6 7 public class LambdaShow 8 { 9 10 public static void Show() 11 { 12 { 13 NoReturnWithPara method = new NoReturnWithPara(ShowStudent);//2 实例化委托 要求传递的方法必须和委托约束的签名一致 14 method.Invoke(196, "Emrys");//3 调用委托 15 } 16 17 { 18 NoReturnWithPara method = ShowStudent;//实例化委托的时候,可以去掉 new NoReturnWithPara 19 method.Invoke(196, "Emrys"); 20 } 21 22 { 23 NoReturnWithPara method = delegate(int id, string name)//匿名方法 24 { 25 Console.WriteLine("id={0} name={1}正在高级班学习....", id, name); 26 }; 27 method.Invoke(196, "Emrys"); 28 } 29 30 { 31 NoReturnWithPara method = (int id, string name) =>//lambda表达式 本质是一个匿名函数,是一个函数 32 { 33 Console.WriteLine("id={0} name={1}正在高级班学习....", id, name); 34 }; 35 method.Invoke(196, "Emrys"); 36 } 37 38 { 39 NoReturnWithPara method = (id, name) =>//实例化委托 要求传递的方法必须和委托约束的签名一致 编译器会自动推断 40 { 41 Console.WriteLine("id={0} name={1}正在高级班学习....", id, name); 42 }; 43 method.Invoke(196, "Emrys"); 44 } 45 46 { 47 //如果方法体只有一行,可以去掉大括号 48 NoReturnWithPara method = (id, name) => Console.WriteLine("id={0} name={1}正在高级班学习....", id, name); 49 method.Invoke(196, "Emrys"); 50 } 51 52 { 53 NoReturnNoPara method = () => { }; 54 NoReturnWithOnePara method1 = i => Console.WriteLine(i); 55 method1.Invoke(13); 56 57 } 58 { 59 WithReturnWithPara method = (m, n) => { return DateTime.Now.Second; }; 60 Console.WriteLine("当前时间的second={0}", method.Invoke(3, 4)); 61 } 62 { 63 WithReturnWithPara method = (m, n) => DateTime.Now.Second; 64 Console.WriteLine("当前时间的second={0}", method.Invoke(3, 4)); 65 } 66 67 68 { 69 //0 到16个参数 无返回值 的泛型委托 70 Action method = () => { }; 71 Action<int> act1 = i => Console.WriteLine(i); 72 Action<string> act2 = i => Console.WriteLine(i); 73 Action<string, long, DateTime, string, int, decimal, byte, LambdaShow, long, DateTime, string, int, decimal, byte, LambdaShow, Program> act1111 = null; 74 Action<string, string, long, DateTime, string, int, decimal, byte, LambdaShow, long, DateTime, string, int, decimal, byte, LambdaShow, Program> act11111 = null; 75 } 76 { 77 //0 到16个参数 带返回值 的泛型委托 78 Func<string> func1 = () => "????????Menzer"; 79 Console.WriteLine(func1.Invoke()); 80 Func<string, string, long, DateTime, string, int, decimal, byte, LambdaShow, long, DateTime, string, int, decimal, byte, LambdaShow, Program> func1111 = null; 81 Func<string, string, string, long, DateTime, string, int, decimal, byte, LambdaShow, long, DateTime, string, int, decimal, byte, LambdaShow, Program> func11112 = null; 82 } 83 84 85 86 87 } 88 89 public delegate void NoReturnNoPara();//1 声明委托 90 public delegate void NoReturnWithOnePara(int id); 91 public delegate void NoReturnWithOnePara1(string id); 92 public delegate void NoReturnWithOnePara2(long id); 93 public delegate void NoReturnWithOnePara3(DateTime id); 94 public delegate void NoReturnWithPara(int id, string name); 95 public delegate int WithReturnNoPara(); 96 public delegate int WithReturnWithPara(int x, int y); 97 98 99 100 101 private static void ShowStudent(int id, string name) 102 { 103 Console.WriteLine("id={0} name={1}正在高级班学习....", id, name); 104 } 105 private static void ShowStudent1(int id, string name) 106 { 107 Console.WriteLine("id={0} name={1}正在高级班学习....", id, name); 108 } 109 private static void ShowStudent2(int id, string name) 110 { 111 Console.WriteLine("id={0} name={1}正在高级班学习....", id, name); 112 } 113 private static void ShowStudent3(int id, string name) 114 { 115 Console.WriteLine("id={0} name={1}正在高级班学习....", id, name); 116 } 117 } 118 }
标签:
原文地址:http://www.cnblogs.com/cinlap/p/5697714.html