Following code won’t compile:
class Test<T> where T : class, IComparable
{
public bool IsGreater(T t1, T t2)
{
return t1 > t2; // Cannot apply operator '>' to operands of type 'T' and 'T'
}
}
How I can make it work? I want it to work for T = int, double, DateTime, etc.
>Solution :
The IComparable interface that you have used here as the type constraint gives you a CompareTo method. So you can do something like this:
public bool IsGreater(T t1, T t2)
{
return t1.CompareTo(t2) > 0;
}