1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleDemo.delegateUtil 8 { 9 public static class DelegateDemo 10 { 11 public delegate string Query(string para1, int para2); 12 13 public delegate string Select(string p1, string p2, string p3, string p4, string p5, string p6, 14 string p7, string p8, string p9, string p10, string p11, string p12, 15 string p13, string p14, string p15, string p16, string p17, string p18); 16 17 public static void Get() 18 { 19 Console.WriteLine("********************************"); 20 21 { 22 //.net framework 1.0 23 Query method = new Query(QueryUser); 24 var value = method.Invoke("sun", 10); 25 Console.WriteLine(value);//返回用户:sun, 年龄:10 26 } 27 28 { 29 //2.0 30 Query method = new Query(delegate (string para1, int para2) 31 { 32 33 return "返回用户:" + para1 + ", 年龄:" + para2; 34 }); 35 36 string value = method.Invoke("sun", 20); 37 Console.WriteLine(value);//返回用户:sun, 年龄:20 38 } 39 40 { 41 //3.0 42 Query method = new Query((string para1, int para2) => 43 { 44 return "返回用户:" + para1 + ", 年龄:" + para2; 45 }); 46 string value = method.Invoke("sun", 30); 47 Console.WriteLine(value);//返回用户:sun, 年龄:30 48 } 49 50 { 51 //c#内置委托,带有一个string参数的,无返回值的匿名方法 52 Action<string> method = (string para1) => 53 { 54 Console.WriteLine("hello " + para1 + " , age : 40"); 55 }; 56 method.Invoke("sun"); 57 } 58 { 59 60 //Action 最多16个参数 61 Action<string, string, string, string, string, 62 string, string, string, string, string, 63 string, string, string, string, string, 64 string> method = (p1, p2, p3, p4, p5, 65 p6, p7, p8, p9, p0, 66 pq, pw, pe, pr, pp, pl) => 67 { 68 Console.WriteLine("这里就不输出16个参数的值了"); 69 }; 70 method.Invoke("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "17", "18"); 71 } 72 { 73 74 //扩展 17个参数如何设置 75 //自己声明一个带有17个参数的委托 76 Select method = (p1, p2, p3, p4, p5, 77 p6, p7, p8, p9, p10, 78 p11, p12, p13, p14, p15, p16, p17, p18) => 79 { 80 81 return "这里是带有18个参数的值"; 82 }; 83 var value = method.Invoke("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "17", "18"); 84 Console.WriteLine(value); 85 } 86 87 { 88 //内置委托,有参 89 Func<int, string> method = (id) => 90 { 91 92 return "我通过传递" + id + ",返回string的值"; 93 }; 94 var value = method.Invoke(50); 95 Console.WriteLine(value); 96 } 97 98 { 99 //linq 100 User u = UserList().Find(t => t.Id == 1); 101 Console.WriteLine("linq自带的方法:" + u.Name); 102 103 //自己定义一个 104 User tmp = UserList().FindByDemo(t => t.Id == 1); 105 Console.WriteLine("自己手写方法:" + tmp.Name); 106 107 108 //思考 场景:查询语句会出现多条件的查询(面试中有被问到),如何做到不通过if else判断 109 IEnumerable<User> list = null; 110 List<User> datasource = UserList(); 111 if (1 == 0) //假设过滤条件为用户年纪大于20 112 { 113 list = datasource.Where(t => t.Age > 20); 114 } 115 116 if (1 == 1) //假设过滤条件为用户名字包含i的用户 117 { 118 list = datasource.Where(t => t.Name.IndexOf("i") >= 0); 119 } 120 121 //以上场景写法 122 123 //合理写法: 124 125 } 126 { 127 //linq to object 之IEnumerable 操作内存 使用内置委托 128 //linq to sql 之 IQueryable 拼接sql?? 表达式目录树,二叉树 129 } 130 Console.WriteLine("********************************"); 131 } 132 133 134 private static string QueryUser(string Name, int age) 135 { 136 return "返回用户:" + Name + ", 年龄:" + age; 137 } 138 139 140 private static List<User> UserList() 141 { 142 143 144 145 List<User> tmp = new List<User>(); 146 147 tmp.Add(new User() { Id = 1, Age = 10, Name = "sun" }); 148 tmp.Add(new User() { Id = 2, Age = 20, Name = "bob" }); 149 tmp.Add(new User() { Id = 3, Age = 25, Name = "justin" }); 150 tmp.Add(new User() { Id = 4, Age = 30, Name = "linda" }); 151 tmp.Add(new User() { Id = 5, Age = 40, Name = "lucy" }); 152 tmp.Add(new User() { Id = 6, Age = 50, Name = "momo" }); 153 tmp.Add(new User() { Id = 7, Age = 60, Name = "any" }); 154 tmp.Add(new User() { Id = 8, Age = 70, Name = "lily" }); 155 156 return tmp; 157 158 } 159 160 public static T FindByDemo<T>(this List<T> list, Func<T, bool> expression) where T : class, new() 161 { 162 T t = new T(); 163 foreach (var u in list) 164 { 165 if (expression.Invoke(u)) 166 { 167 t = u; 168 } 169 } 170 171 return t; 172 } 173 174 } 175 176 public class User 177 { 178 179 public int Id { get; set; } 180 public int Age { get; set; } 181 public string Name { get; set; } 182 } 183 }