/// <summary> /// 根据 比较方法找最大值 /// </summary> /// <returns></returns> object GetMax(object[] values, DGCompare comparor) { object max = values[0]; foreach (object obj in values) { if (comparor.Invoke(obj, max) > 0) { max = obj; } } return(max); }
private void btnFindMaxByDel_Click(object sender, EventArgs e) { DGCompare dg = new DGCompare(IntCompare); //找最大数值 object[] arrInt = { 5, 4, 1, 9, 8 }; int maxInt = Convert.ToInt32(GetMax(arrInt, new DGCompare(IntCompare))); //找最长字符串 object[] arrStr = { "ui", "xiaomi", "lei", "h" }; string maxLenStr = GetMax(arrStr, StringCompare).ToString(); //找年龄最大的狗 object[] arrDog = { new Dog { name = "小白", age = 21 }, new Dog{ name = "小黑", age = 11 }, new Dog{ name = "小花", age = 5 } }; Dog oldestDog = GetMax(arrDog, DogCompare) as Dog; }