Search code examples
c#.netcomparisonnullableicomparable

Reasons to not throw when implementing `IComparable<T>`?


Are there any arguments against throwing inside the implementation of IComparable<T>.CompareTo(T value) ?

Does it make sense that Nullable<T>.Compare(null, notNullValue) doesn't throw and why ?


Solution

  • The reasons for not throwing are that it's not needed and that the signature of IComparable.CompareTo(T) Method doesn't specify any exceptions, so you'd break the contract.

    (BTW. IComparable.CompareTo(Object) Method does allow ArgumentExecption).


    Does it make sense that Nullable.Compare(null, notNullValue) doesn't throw and why ?

    Yes it makes sense not to throw, as null value has its place in the order of things. For example if you have a nullable column in the database you can still order it. In SELECT - ORDER BY Clause (Transact-SQL) you can read:

    ASC | DESC

    Specifies that the values in the specified column should be sorted in ascending or descending order. ASC sorts from the lowest value to highest value. DESC sorts from highest value to lowest value. ASC is the default sort order. Null values are treated as the lowest possible values. [emphasis mine]