在朋友的代码上修改的,在此感谢我的朋友---邓建军
public delegate int CompareDelegate(object max, object obj);
class Program{
static void Main(string[] args)
{
Console.WriteLine(GetMaxObject(new object[] { 28, 45 }, CompareAge));
Console.WriteLine("ok");
Console.ReadKey();
}
//写一个通用的计算最大值的方法,这个一定要是static才能被调用
public static object GetMaxObject(object[] objs, CompareDelegate BiJiaoFangShi)
{
object max = objs[0];
for (int i = 1; i < objs.Length; i++)
{
//假设BiJiaoFangShi,的返回值是整型,如果返回大于0的数,表示max大于objs[i],如果返回负数,表示max<objs[i],如果返回0,表示max==objs[i]
if (BiJiaoFangShi(max, objs[i]) < 0)
{
max = objs[i];
}
}
return max;
}
private static int CompareAge(Object max, Object obj)
{
int n1 = Convert.ToInt32(max);
int n2 = Convert.ToInt32(obj);
return n1 - n2;
}
}