needs overload operator< and null check

后端 未结 5 1417
暖寄归人
暖寄归人 2020-12-18 21:54

I´m overloading the lessthan-operator in c# and I`m wondering whether this needs to check for null. Below you can find an Example:

public static bool operat         


        
5条回答
  •  再見小時候
    2020-12-18 22:14

    The answer depends on your intended usage pattern. If you plan to have nulls in the mix, and you would like to consider null values to be less than non-null values, then your implementation is correct; if you would like to consider null values to be greater than non-null objects, then the commented out return values (false and true) should be used instead. If you do not plan to allow nulls in the mix, throwing an ArgumentNullException or allowing NullReferenceException would be the right choice.

提交回复
热议问题