Trying to define a class restriction, but get an error:
public class Utilities3<T> where T : IComparable
{
public T Max<T>(T a, T b)
{
return a.CompareTo(b) > 0 ? a : b;
}
}
'T' does not contain a definition for "CompareTo", and the extension method "CompareTo" cannot be found that takes the first argument of type "T" (do you miss the using directive or assembly references?)
Unable to resolve CompareTo character

Function restriction works fine:
public class Utilities2<T>
{
public T Max<T>(T a, T b) where T : IComparable
{
return a.CompareTo(b) > 0 ? a : b;
}
}
Why doesn't class restriction work?
vico source
share