委托、泛型委托、多播委托 和 lamda表达式
委托基本概念:可以把一个方法作为一个参数传给另一个方法
声明: 方法前加 delegate 关键字
系统封装action 无返回值无参数 , action<T>无返回值有参数,FUNC<x,x,x>有返回值最后一个是返回值
列子:
using System; using System.Collections; using System.Collections.Generic; namespace Dome { class dom { static void Main(string[] args) { string[] sname = { "Abc", "dFg", "HjK" }; toUpper(sname); for (int i = 0; i < sname.Length;i++ ) { Console.WriteLine(sname[i]); } Console.ReadKey(); } //现在有三个需求 //将字符串数组中的每个元素转换成大写; //将字符串数组中的每个元素转换成小写; //将字符串数组中的每个元素都添加上一个双引号; public static void toUpper(string[] names) { for (int i = 0; i <names.Length ;i++ ) { names[i] = names[i].ToUpper(); } } //xiaoxie public static void toxiao(string[] names) { for (int i = 0; i < names.Length;i++ ) { names[i] = names[i].ToLower(); } } //添加双引号 public static void toshuang(string[] names) { for (int i = 0; i < names.Length; i++) { names[i] = "\""+names[i]+"\""; } } } }
委托:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace cs { public delegate void delsayhi(string name);//放在类外面 class t { static void Main(string[] args) { //声明一个委托,必须要跟传递的方法具有相同的 签名(返回值和参数) //创建委托对象 //函数可以赋值给一个委托 delsayhi del = wsayhi; //new delsayhi(wsayhi); del("张三"); test("张张",sayhi); //匿名函数 没有名字的函数 test("李四", delegate(string name) { Console.WriteLine("hello"+name); }); //lamda表达式 => goes to test("王五", (string name) => { Console.WriteLine("hello" + name); }); Console.ReadKey(); } public static void test(string name, delsayhi del) { del(name); } public static void sayhi(string name) { Console.WriteLine("你好" + name); } public static void wsayhi(string name) { Console.WriteLine("hello" + name); } } }
使用委托简化:
using System; using System.Collections; using System.Collections.Generic; namespace Dome { public delegate string delpro(string str); class dom { static void Main(string[] args) { string[] sname = { "Abc", "dFg", "HjK" }; //toUpper(sname); test(sname, p3); for (int i = 0; i < sname.Length;i++ ) { Console.WriteLine(sname[i]); } Console.ReadKey(); } public static void test(string[] names,delpro del) { for (int i = 0; i < names.Length;i++ ) { names[i] = del(names[i]); } } public static string p1(string str) { return str.ToUpper(); } public static string p2(string str) { return str.ToLower(); } public static string p3(string str) { return "\""+str+"\""; } //现在有三个需求 //将字符串数组中的每个元素转换成大写; //将字符串数组中的每个元素转换成小写; //将字符串数组中的每个元素都添加上一个双引号; //public static void toUpper(string[] names) { // for (int i = 0; i <names.Length ;i++ ) // { // names[i] = names[i].ToUpper(); // } //} ////xiaoxie //public static void toxiao(string[] names) { // for (int i = 0; i < names.Length;i++ ) { // names[i] = names[i].ToLower(); // } //} ////添加双引号 //public static void toshuang(string[] names) { // for (int i = 0; i < names.Length; i++) // { // names[i] = "\""+names[i]+"\""; // } //} } }
使用匿名函数再简化:
using System; using System.Collections; using System.Collections.Generic; namespace Dome { public delegate string delpro(string str); class dom { static void Main(string[] args) { string[] sname = { "Abc", "dFg", "HjK" }; //toUpper(sname); // test(sname, p3); //使用匿名函数简化; test(sname, delegate(string name) { return name.ToUpper(); }); //lamda表达式 test(sname, (string str) => { return str.ToLower(); }); for (int i = 0; i < sname.Length;i++ ) { Console.WriteLine(sname[i]); } Console.ReadKey(); } public static void test(string[] names,delpro del) { for (int i = 0; i < names.Length;i++ ) { names[i] = del(names[i]); } } //public static string p1(string str) { // return str.ToUpper(); //} //public static string p2(string str) { // return str.ToLower(); //} //public static string p3(string str) { //return "\""+str+"\""; //} } }
泛型委托:
using System; using System.Collections; using System.Collections.Generic; namespace Dome//求数组的最大值 { public delegate int delpro(object str1,object str2); class dom { static void Main(string[] args) { object[] o = { 1,2,3,4,5,6}; object max = getmax(o,test); Console.WriteLine(max); Console.ReadKey(); } //数组最大值 public static object getmax(object[] nums,delpro del)//int改为object { object max = nums[0]; for (int i = 0; i < nums.Length;i++ ) { if (del(max, nums[i]) < 0)// 替换 nums[i] > max { max=nums[i]; } } return max; } public static int test(object o1, object o2) { int n1 = (int)o1; int n2 = (int)o2; return n1 - n2; } // public static int test(string n1,string n2) {return n1.Length-n2.Length; } //字符串长度最大值 //public static string getmax(string[] names) { // string max = names[0]; // for (int i = 0; i < names.Length;i++ ) { // if(names[i].Length>max.Length ){ // max=names[i]; // } // } // return max; //} } }
使用泛型委托简化
using System; using System.Collections; using System.Collections.Generic; namespace Dome//求数组的最大值 { public delegate int delpro<T>(T str1,T str2); class dom { static void Main(string[] args) { int[] o = { 1,2,3,4,5,6}; string[] s = { "we123ewr", "123df4rt", "32wear234rwer23" }; int max = getmax<int>(o,test); string smax=getmax<string>(s,test); Console.WriteLine(max); Console.WriteLine(smax); Console.ReadKey(); } //数组最大值 public static T getmax<T>(T[] nums,delpro<T> del)//int改为object { T max = nums[0]; for (int i = 0; i < nums.Length;i++ ) { if (del(max, nums[i]) < 0)// 替换 nums[i] > max { max=nums[i]; } } return max; } public static int test(int n1, int n2) { return n1 - n2; } public static int test(string n1,string n2) { return n1.Length - n2.Length; } } }
using System; using System.Collections; using System.Collections.Generic; namespace Dome//求数组的最大值 { public delegate void sayhi(string str); class dom { static void Main(string[] args) { sayhi hi = hi1; hi += hi2;//多播委托 hi += hi3; hi("张三"); Console.WriteLine(); Console.ReadKey(); } public static void hi1(string name) { Console.WriteLine("hi1"+name); } public static void hi2(string name) { Console.WriteLine("hi2" + name); } public static void hi3(string name) { Console.WriteLine("hi3" + name); } } }
多播输出结果
lamda表达式
using System; using System.Collections; using System.Collections.Generic; namespace Dome//求数组的最大值 { public delegate void sayhi(string str); class dom { static void Main(string[] args) { List<int> list = new List<int>(); list.AddRange(new int[]{1,2,3,4,5,6}); list.RemoveAll(a=>a>3);//使用lamda表达式(=>)移除大于3的数字 foreach(int i in list){ Console.WriteLine(i); } Console.ReadKey(); } } }
时间就像海绵里的水,只要你愿意挤,总还是有的——鲁迅