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

后端 未结 13 2164
清酒与你
清酒与你 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 10:01

    Well, it's probably just a design choice, but as you say, x!= y doesn't have to be the same as !(x == y). By not adding a default implementation, you are certain that you cannot forget to implement a specific implementation. And if it's indeed as trivial as you say, you can just implement one using the other. I don't see how this is 'poor practise'.

    There may be some other differences between C# and Lua too...

提交回复
热议问题