-1

I'm new to C# and because of this I found an annoying problem which stop me from making my homework. What's the difference between IComparable and IComparable<K>?

One of my friend used something like this Vector_d<T> where T:IComparable, but everywhere else in source files he used T:IComparable<T>. When I asked him, he didn't know to answer so I have to search somewhere else.

Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31

1 Answers1

4

The first one, IComparable, is from the old .NET days, where there were no generics. It basically allows you to compare any type implementing it to any other type, but possibly it will throw an exception, if the types are not comparable. The generic version, IComparable<>, will only let you compare two values of the same type (or of a derived type), most likely what you want. Old types, such as Int32, used to implement only IComparable, but now they implement both, but IComparable privately, so as not to be called instead of the generic version.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74