needs overload operator< and null check

后端 未结 5 1438
暖寄归人
暖寄归人 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:17

    A custom operator is little more than a static method. Moreover, operators in generals shouldn't normally throw exceptions. Which means you need those null-checks if MyClass is a reference-type.

    By the way, it's conventional for nulls to be less than non-nulls, which makes your proposed implementation idiomatic.

提交回复
热议问题