Is it possible to easily compile the C # compiler so that enumeration types implement the common interfaces IEquatable`1 and IComparable`1?

Suppose I wrote the following method:

public static T Max<T>(T x, T y) where T : struct, IComparable<T>
{
  return (x.CompareTo(y) > 0) ? x : y;
}

This works fine and finds the larger value from the two value values. Here is an example where it was used:

void Test(DateTime oneTime, DateTime anotherTime)
{
  var latestTime = Max(oneTime, anotherTime);
  ...
}

But the above method does not work for enumerations. So, for example, Max(DayOfWeek.Thursday, DayOfWeek.Friday)it will not work due to a limitation (compiler :) "There is no boxing conversion from 'System.DayOfWeek' to 'System.IComparable<System.DayOfWeek>'.".

There are, of course, workarounds. I could force the restriction to refer to a non-common interface, but CompareToincluded a box from yto in the call System.Object. And who likes non-common .NET 1 interfaces?

DayOfWeek IComparable ( System.Enum ), , , DayOfWeek IComparable<DayOfWeek>.

, : ( .NET), #, enum MyFoo { ... }, , IComparable<MyFoo> IEquatable<MyFoo>?

( .NET 2, T[] IList<T>, System.Array . "" (System.Enum , "" ).)

+5

All Articles