If you only want to create comparison functions then you could use the default comparer for the type T
. For example:
public static T Max<T>(T x, T y)
{
return (Comparer<T>.Default.Compare(x, y) > 0) ? x : y;
}
If T
implements IComparable<T>
then that comparer will be used; if T
doesn't implement IComparable<T>
but does implement IComparable
then that comparer will be used; if T
doesn't implement either IComparable<T>
or IComparable
then a runtime exception will be thrown.
If you want/need to do more than just compare the items then you could have a look at the generic operators implementation in MiscUtil and the related article.