1.1 找出int数组中的最大值,我们可能会这样写
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Delegate_GetMax { class Program { static void Main(string[] args) { int[] ins = {1,2,33,44,3421,1,5,2,9,6 }; int max = GetMax(ins); Console.WriteLine(max); Console.Read(); } private static int GetMax(int[] ins) { int max = ins[0]; foreach (int item in ins) { if (max < item) { max = item; } } return max; } } }
1.2 如果要找出string数组的最大长度呢?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Delegate_GetMax { class Program { static void Main(string[] args) { int[] ins = {1,2,33,44,3421,1,5,2,9,6 }; string[] strs = { "asdf",";ljsdfasldj","uasjaldf","s","asdfasdf"}; int max = GetMax(ins); string maxStr = GetMax(strs); Console.WriteLine(max); Console.WriteLine(maxStr); Console.Read(); } private static int GetMax(int[] ins) { int max = ins[0]; foreach (int item in ins) { if (max < item) { max = item; } } return max; } private static string GetMax(string[] strs) { int max = strs[0].Length; string str = strs[0]; foreach (string item in strs) { if (max < item.Length) { max = item.Length; str = item; } } return str; } } }
1.3 我们可以把它们以委托的方式写出来
通过分析可以发现,两种比较方法在调用具体比较时所用的方法是不同,而且如果要统一写的化应该用object[]代替
1.3.1 声明委托,用于比较两个对象,返回值是int ,例如传入两个string,通过内部比较返回是否>0,以区别两个,字符串类型的长度.
public delegate int delCompare(object o1,object o2);
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Delegate_GetMax { public delegate int delCompare(object o1,object o2); class Program { static void Main(string[] args) { object[] ins = {1,2,33,44,3421,1,5,2,9,6 }; object[] strs = { "asdf",";ljsdfasldj","uasjaldf","s","asdfasdf"}; //比较整形数组 delCompare del = new delCompare(IntCompare); object max = GetMax(ins, del); //比较字符串 object maxStr = GetMax(strs, StringCompare); Console.WriteLine(max); Console.WriteLine(maxStr); Console.Read(); } private static object GetMax(object[] objs,delCompare delc) { object max = objs[0]; foreach (object item in objs) { if (delc(max,item) < 0) { max = item; } } return max; } private static int IntCompare(object o1,object o2) { return (int)o1 - (int)o2; } private static int StringCompare(object o1, object o2) { return o1.ToString().Length - o2.ToString().Length; } } }
1.4 进一步升级
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Delegate_GetMax { public delegate int delCompare(object o1,object o2); class Program { static void Main(string[] args) { object[] ins = {1,2,33,44,3421,1,5,2,9,6 }; object[] strs = { "asdf",";ljsdfasldj","uasjaldf","s","asdfasdf"}; //比较整形数组 // delCompare del = new delCompare(IntCompare); //1.0 通过定义委托调用方法 //object max = GetMax(ins, del); object max = GetMax(ins, (o1, o2) => { return (int)o1 - (int)o2; }); //比较字符串 // object maxStr = GetMax(strs, StringCompare); //2.0 直接调用方法 // object maxStr = GetMax(strs, delegate(object o1, object o2) { return o1.ToString().Length - o2.ToString().Length; });//3.0 通过匿名方法 object maxStr = GetMax(strs, (o1, o2) => { return o1.ToString().Length - o2.ToString().Length; }); //4.0 lmadba表达式 Console.WriteLine(max); Console.WriteLine(maxStr); Console.Read(); } private static object GetMax(object[] objs,delCompare delc) { object max = objs[0]; foreach (object item in objs) { if (delc(max,item) < 0) { max = item; } } return max; } } }
1.5 那么现在如果加一个Person类呢?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Delegate_GetMax { public class Person { public String Name { get; set; } public int Age { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Delegate_GetMax { public delegate int delCompare(object o1,object o2); class Program { static void Main(string[] args) { object[] ins = {1,2,33,44,3421,1,5,2,9,6 }; object[] strs = { "asdf",";ljsdfasldj","uasjaldf","s","asdfasdf"}; Person[] perons = { new Person { Name = "张三", Age = 40 }, new Person { Name = "李四", Age = 50 }, new Person { Name = "王五", Age = 80 } }; //比较整形数组 // delCompare del = new delCompare(IntCompare); //1.0 通过定义委托调用方法 //object max = GetMax(ins, del); object max = GetMax(ins, (o1, o2) => { return (int)o1 - (int)o2; }); //比较字符串 // object maxStr = GetMax(strs, StringCompare); //2.0 直接调用方法 // object maxStr = GetMax(strs, delegate(object o1, object o2) { return o1.ToString().Length - o2.ToString().Length; });//3.0 通过匿名方法 object maxStr = GetMax(strs, (o1, o2) => { return o1.ToString().Length - o2.ToString().Length; }); //4.0 lmadba表达式 //比较Person object maxPeople = GetMax(perons,(o1,o2) =>{ Person p1 = (Person)o1 ;Person p2 = (Person)o2 ;return p1.Age - p2.Age;}); Person pMax = (Person)maxPeople; Console.WriteLine(max); Console.WriteLine(maxStr); Console.WriteLine(pMax.Name); Console.Read(); } private static object GetMax(object[] objs,delCompare delc) { object max = objs[0]; foreach (object item in objs) { if (delc(max,item) < 0) { max = item; } } return max; } } }
1.6 继续观察发现
object max = GetMax(ins, (o1, o2) => { return (int)o1 - (int)o2; });
object maxStr = GetMax(strs, (o1, o2) => { return o1.ToString().Length - o2.ToString().Length; }); //4.0 lmadba表达式
object maxPeople = GetMax(perons,(o1,o2) =>{ Person p1 = (Person)o1 ;Person p2 = (Person)o2 ;return p1.Age - p2.Age;});
这其中都有类型转换,装箱拆箱比较麻烦
1.7 泛型委托
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Delegate_GetMax { public delegate int delCompare<T>(T o1,T o2); class Program { static void Main(string[] args) { int[] ins = {1,2,33,44,3421,1,5,2,9,6 }; string[] strs = { "asdf",";ljsdfasldj","uasjaldf","s","asdfasdf"}; Person[] perons = { new Person { Name = "张三", Age = 40 }, new Person { Name = "李四", Age = 50 }, new Person { Name = "王五", Age = 80 } }; //比较整形数组 // delCompare del = new delCompare(IntCompare); //1.0 通过定义委托调用方法 //object max = GetMax(ins, del); int max = GetMax(ins, (o1, o2) => { return o1 - o2; }); //比较字符串 // object maxStr = GetMax(strs, StringCompare); //2.0 直接调用方法 // object maxStr = GetMax(strs, delegate(object o1, object o2) { return o1.ToString().Length - o2.ToString().Length; });//3.0 通过匿名方法 string maxStr = GetMax(strs, (o1, o2) => { return o1.Length - o2.Length; }); //4.0 lmadba表达式 //比较Person object maxPeople = GetMax(perons,(o1,o2) =>{return o1.Age - o2.Age;}); Person pMax = (Person)maxPeople; Console.WriteLine(max); Console.WriteLine(maxStr); Console.WriteLine(pMax.Name); Console.Read(); } private static T GetMax<T>(T[] objs,delCompare<T> delc) { T max = objs[0]; foreach (T item in objs) { if (delc(max,item) < 0) { max = item; } } return max; } } }