using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TDelegate
{
delegate int DelCompare<T>(T t1, T t2);
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, 25, 262, 354, 51, 253, 64, 225, 643 };
int max = GetMax<int>(nums, Compare);
Console.WriteLine("int数组中最大值是:" + max);
string[] strs = { "adsgd", "werr", "sad", "", "asdfsafd" };
string str = GetMax<string>(strs, delegate(string s1, string s2)
{
return s1.Length - s2.Length;
});
Console.WriteLine("字符串数组中最大值是:" + str);
Console.ReadKey();
}
public static T GetMax<T>(T[] nums, DelCompare<T> del)
{
T max = nums[0];
for (int i = 0; i < nums.Length; i++)
{
if (del(max, nums[i]) < 0)
{
max = nums[i];
}
}
return max;
}
public static int Compare(int n1, int n2)
{
return n1 - n2;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TDelegate
{
delegate int DelCompare<T>(T t1, T t2);
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, 25, 262, 354, 51, 253, 64, 225, 643 };
int max = GetMax<int>(nums, Compare);
Console.WriteLine("int数组中最大值是:" + max);
string[] strs = { "adsgd", "werr", "sad", "", "asdfsafd" };
string str = GetMax<string>(strs, delegate(string s1, string s2)
{
return s1.Length - s2.Length;
});
Console.WriteLine("字符串数组中最大值是:" + str);
Console.ReadKey();
}
public static T GetMax<T>(T[] nums, DelCompare<T> del)
{
T max = nums[0];
for (int i = 0; i < nums.Length; i++)
{
if (del(max, nums[i]) < 0)
{
max = nums[i];
}
}
return max;
}
public static int Compare(int n1, int n2)
{
return n1 - n2;
}
}
}