using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateGetMax
{
delegate int DelMax(object o1, object o2);
class Program
{
/// <summary>
/// 使用委托求一个不确定类型数组的最大值
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
实用委托求字符串数组的最大值
//object[] str = { "2354tqwafasg", "asgtsa345346t", "w3r3wrsdat4t", "sdgq4w4t46tsdf", "asdgfaweestgwe3", "dagerwet245twreq", "sdfasdfasdfd" };
//object result = GetMax(str, delegate(object o1, object o2)
//{
// string s1 = (string)o1;
// string s2 = (string)o2;
// return s1.Length - s2.Length;
//});
//Console.WriteLine(result);
//实用匿名函数求字符串类型的数字求最大值
//string[] str = { "2354tqwafasg", "asgtsa345346t", "w3r3wrsdat4t", "sdgq4w4t46tsdf", "asdgfaweestgwe3", "dagerwet245twreq", "sdfasdfasdfd" };
//object result = GetMax(str, (object o1, object o2) =>
//{
// string s1 = (string)o1;
// string s2 = (string)o2;
// return s1.Length - s2.Length;
//});
//Console.WriteLine(result);
//使用委托求int类型的数组求最大值
//object[] i = { 124, 15245, 123, 36, 364, 74, 325, 743, 234 };
//object result = GetMax(i, delegate(object o1, object o2)
//{
// int i1 = (int)o1;
// int i2 = (int)o2;
// return i1 - i2;
//});
//Console.WriteLine(result);
//实用匿名函数求int类型的数字求最大值
object[] i = { 124, 15245, 123, 36, 364, 74, 325, 743, 234 };
object result = GetMax(i, (object o1, object o2) =>
{
int i1 = (int)o1;
int i2 = (int)o2;
return i1 - i2;
});
Console.WriteLine(result);
Console.ReadKey();
}
public static object GetMax(object[] o, DelMax del)
{
object max = o[0];
for (int i = 0; i < o.Length; i++)
{
if (del(max, o[i]) < 0)
{
max = o[i];
}
}
return max;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateGetMax
{
delegate int DelMax(object o1, object o2);
class Program
{
/// <summary>
/// 使用委托求一个不确定类型数组的最大值
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
实用委托求字符串数组的最大值
//object[] str = { "2354tqwafasg", "asgtsa345346t", "w3r3wrsdat4t", "sdgq4w4t46tsdf", "asdgfaweestgwe3", "dagerwet245twreq", "sdfasdfasdfd" };
//object result = GetMax(str, delegate(object o1, object o2)
//{
// string s1 = (string)o1;
// string s2 = (string)o2;
// return s1.Length - s2.Length;
//});
//Console.WriteLine(result);
//实用匿名函数求字符串类型的数字求最大值
//string[] str = { "2354tqwafasg", "asgtsa345346t", "w3r3wrsdat4t", "sdgq4w4t46tsdf", "asdgfaweestgwe3", "dagerwet245twreq", "sdfasdfasdfd" };
//object result = GetMax(str, (object o1, object o2) =>
//{
// string s1 = (string)o1;
// string s2 = (string)o2;
// return s1.Length - s2.Length;
//});
//Console.WriteLine(result);
//使用委托求int类型的数组求最大值
//object[] i = { 124, 15245, 123, 36, 364, 74, 325, 743, 234 };
//object result = GetMax(i, delegate(object o1, object o2)
//{
// int i1 = (int)o1;
// int i2 = (int)o2;
// return i1 - i2;
//});
//Console.WriteLine(result);
//实用匿名函数求int类型的数字求最大值
object[] i = { 124, 15245, 123, 36, 364, 74, 325, 743, 234 };
object result = GetMax(i, (object o1, object o2) =>
{
int i1 = (int)o1;
int i2 = (int)o2;
return i1 - i2;
});
Console.WriteLine(result);
Console.ReadKey();
}
public static object GetMax(object[] o, DelMax del)
{
object max = o[0];
for (int i = 0; i < o.Length; i++)
{
if (del(max, o[i]) < 0)
{
max = o[i];
}
}
return max;
}
}
}