Why must we define both == and != in C#?

后端 未结 13 2211
清酒与你
清酒与你 2020-11-27 08:55

The C# compiler requires that whenever a custom type defines operator ==, it must also define != (see here).

Why?

I\'m curious to k

13条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 09:58

    This is what comes to my mind first:

    • What if testing inequality is much faster than testing equality?
    • What if in some cases you want to return false both for == and != (i.e. if they can't be compared for some reason)

提交回复
热议问题