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

后端 未结 13 2185
清酒与你
清酒与你 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条回答
  •  Happy的楠姐
    2020-11-27 09:46

    Probably for if someone needs to implement three-valued logic (i.e. null). In cases like that - ANSI standard SQL, for instance - the operators can't simply be negated depending on the input.

    You could have a case where:

    var a = SomeObject();
    

    And a == true returns false and a == false also returns false.

提交回复
热议问题