4

I'm wondering because the MSDN documentation says the following:

Less than zero: This instance is less than value.

Zero: This instance is equal to value.

Greater than zero: This instance is greater than value.

If you just want to compare a value being less, equal or greater, why not just use -1, 0 and 1?

Steven S.
  • 654
  • 6
  • 10
  • 3
    because the obvious way to compare two integers is to return a - b. – Michael Edenfield Mar 01 '13 at 12:23
  • 1
    @MichaelEdenfield: Not a good way. Compare `int.MinValue` and `int.MinValue + 1` like that and brace for an overflow-related exception, I suppose? – O. R. Mapper Mar 01 '13 at 12:27
  • @MichaelEdenfield: Sorry - of course I meant `int.MinValue` and `int.MaxValue`. – O. R. Mapper Mar 01 '13 at 12:58
  • 1
    related: [Why is CompareTo on short implemented this way?](http://stackoverflow.com/questions/6579966/why-is-compareto-on-short-implemented-this-way) – sloth Mar 01 '13 at 13:06
  • @O.R.Mapper that is a valid concern; however, in most cases where I am implementing my own `IComparable<>` I already have logic to restrict the valid sets of values to something well inside the range of `int`. – Michael Edenfield Mar 01 '13 at 14:20
  • @DominicKexel I guess that question is the real answer to "are there any known implementations"... `System.Int16` :) – Michael Edenfield Mar 01 '13 at 14:22
  • @MichaelEdenfield The answer to the question is short, I mean... [`short`](http://msdn.microsoft.com/en-us/library/ybs77ex4.aspx). never to late for a pun! :-) – sloth Mar 01 '13 at 14:26

4 Answers4

4

This question is probably way to broad but I at least know of many such implementations in use at my company, because I wrote them:

public int CompareTo(Student other)
{
  return this.Grade - student.Grade;
}
Michael Edenfield
  • 27,188
  • 4
  • 77
  • 114
4

There are two things to consider here.

  1. What is documented
  2. What is implemented

The actual implementation might currently always return -1, 0, or 1, never anything else. Note that I have not verified this.

However, the documentation specifies that it is legal to use negative values, and positive values, not just -1 and +1.

This means that for you to write robust code using .CompareTo, you need to handle negative and positive values, not just -1 and +1.

Note that this does not mean that you cannot return -1 and +1 from your own implementations of CompareTo, but you need to be prepared for any other values as well.

For simplicity, in some cases, an implementation might just return the result of subtracting one value from another, and thus you will get something that isn't -1 or +1.

Lasse V. Karlsen
  • 350,178
  • 94
  • 582
  • 779
2

If you will look on CompareTo implementation of such types as Byte, Int16, etc you will see

public int CompareTo(short value)
{
    return (this - value);
}

Int32 or Int64 comparison not implemented same way. UPDATE: as @O.R.Mapper noticed, because of arithmetic overflow.

Sergey Berezovskiy
  • 215,927
  • 33
  • 392
  • 421
  • 3
    Because `short.MinValue - short.MaxValue` is still within the range of `int` - the return type of `CompareTo`, but `int.MinValue - int.MaxValue` and `Int64.MinValue - Int64.MaxValue` are not. – O. R. Mapper Mar 01 '13 at 12:33
1

As I understand it, there are a lot (particularly user-defined ones). MSDN is non-specific because you shouldn't be relying on it, the value that comes out of CompareTo doesn't really matter as long as it is valid. For example comparing two ints is fastest by subtracting them.

A real example, this program prints 88.

static void Main(string[] args)
{
    Int16 a = 100;
    Int16 b = 12;
    Console.WriteLine(a.CompareTo(b));
}

While doing this little investigation I found something that seemed a a little odd at first. Int16 (short) uses this method but Int32 (int) does not and returns -1,0,1. This is probably due to the fact that we are working with integers smaller than the return type (Int32) where the minimum value minus the maximum value will fit.

Daniel Imms
  • 43,032
  • 14
  • 130
  • 152