ref:https://codeantenna.com/a/LqzhR71NJp
Console.WriteLine(Compare<int>.compareGeneric(3,5)); //5
Console.WriteLine(Compare<double>.compareGeneric(3.5, 2.5)); //3.5
Console.WriteLine(Compare<char>.compareGeneric('a' ,'d')); //d
Console.WriteLine(Compare<string>.compareGeneric("peter", "kerry")); //peter
public class Compare<T> where T : IComparable
{
//利用泛型實現比較大小
public static T compareGeneric(T t1, T t2)
{
if (t1.CompareTo(t2) > 0)
{
return t1;
}
else
{
return t2;
}
}
}