30

What should be returned in a CompareTo method when the given object is null?

The MSDN Library shows a example where 1 is returned. But I would have expected to throw an error because comparing to null is not possible.

I expect different opinions to this answer. What could be a best practice approach?

5
  • 1
    What do you mean, comparing to null is "not possible"? All reference types can be compared to null normally. Otherwise you can wrap things with the Nullable class.
    – Geeky Guy
    Jun 10, 2013 at 14:14
  • If you "expect different opinions" then this is not a fit for stack overflow as the FAQ clearly stats that you should avoid asking subjective questions. Jun 10, 2013 at 14:18
  • 1
    the example is valid. As CompareTo returns the sort order of the objects, it is not unexpected to categorize null as the first element Jun 10, 2013 at 14:20
  • 1
    Fyi, the currently accepted answer is not correct.
    – jnm2
    Nov 27, 2015 at 13:39
  • 1
    Thanks @jnm2 marked your answer as correct. Nov 27, 2015 at 15:08

4 Answers 4

46

Yes, there is a best practice. Contrary to what the other answers are saying, there is an expected standard, not just a most popular behavior.

The correct answer is given in the MSDN documentation for IComparable<T>.CompareTo and IComparable.CompareTo:

By definition, any object compares greater than null, and two null references compare equal to each other.

(Contractually, comparing greater is defined as: if a > b then a.CompareTo(b) > 0.)

This expected behavior is also borne out for example in Nullable.Compare<T>. Null always compares as less than a value.

It's also worth noting that for the non-generic compare, mismatching types should not be treated as null:

The parameter, obj, must be the same type as the class or value type that implements this interface; otherwise, an ArgumentException is thrown.


This doesn't impact your question, but be aware, Nullable<T> comparison operators (==, !=, <, <=, >, >=) do not follow the IComparable convention.

When you perform comparisons with nullable types, if the value of one of the nullable types is null and the other is not, all comparisons evaluate to false except for != (not equal). It is important not to assume that because a particular comparison returns false, the opposite case returns true. In the following example, 10 is not greater than, less than, nor equal to null. Only num1 != num2 evaluates to true.

There is also the odd result that (int?)null == (int?)null evaluates to true but (int?)null <= (int?)null does not.

2

The choice is yours. It's not beyond the realm of possibility to imagine a valid use case where I'd compare something to nothing, and want "something" to be seen as greater. But that's why you are overriding it, so you can decide how you want to handle that case.

1

The best practise would depend on your particular case: comparing to null might be possible depending on the object you're comparing.

If I define my object such that null is the lowest possible value for any comparison, then comparing to null is clearly possible and has a well-defined result. In other cases, throwing an exception might make more sense.

Ultimately, this is a (fairly subjective) design question, to which there's not necessarily one answer.

0

CompareTo with null argument affects the case when sorting a list with null items. By returning 1 when the given object is null makes null appear on top of the list when sorted, which is the most popular behavior.

1
  • This isn't a complete answer, however everything it says is true so shouldn't have negative/zero score. It could be clearer public int compareTo(MyType givenObject) { if (givenObject == null) { return 1; } else { ... }
    – karmakaze
    Oct 16, 2021 at 14:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.