Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy must we define both == and != in C#?
    text
    copied!<p>The C# compiler requires that whenever a custom type defines operator <code>==</code>, it must also define <code>!=</code> (see <a href="http://msdn.microsoft.com/en-us/library/ms173147%28v=vs.80%29.aspx" rel="noreferrer">here</a>).</p> <p>Why?</p> <p>I'm curious to know why the designers thought it necessary and why can't the compiler default to a reasonable implementation for either of the operators when only the other is present. For example, Lua lets you define only the equality operator and you get the other for free. C# could do the same by asking you to define either == or both == and != and then automatically compile the missing != operator as <code>!(left == right)</code>.</p> <p>I understand that there are weird corner cases where some entities may neither be equal nor unequal, (like IEEE-754 NaN's), but those seem like the exception, not the rule. So this doesn't explain why the C# compiler designers made the exception the rule.</p> <p>I've seen cases of poor workmanship where the equality operator is defined, then the inequality operator is a copy-paste with each and every comparison reversed and every &amp;&amp; switched to a || (you get the point... basically !(a==b) expanded through De Morgan's rules). That's poor practice that the compiler could eliminate by design, as is the case with Lua.</p> <p>Note: The same holds for operators &lt; > &lt;= >=. I can't imagine cases where you'll need to define these in unnatural ways. Lua lets you define only &lt; and &lt;= and defines >= and > naturally through the formers' negation. Why doesn't C# do the same (at least 'by default')?</p> <p><strong>EDIT</strong></p> <p>Apparently there are valid reasons to allow the programmer to implement checks for equality and inequality however they like. Some of the answers point to cases where that may be nice.</p> <p>The kernel of my question, however, is why this is forcibly required in C# when <em>usually</em> it's not <em>logically</em> necessary?</p> <p>It is also in striking contrast to design choices for .NET interfaces like <code>Object.Equals</code>, <code>IEquatable.Equals</code> <code>IEqualityComparer.Equals</code> where the lack of a <code>NotEquals</code> counterpart shows that the framework considers <code>!Equals()</code> objects as unequal and that's that. Furthermore, classes like <code>Dictionary</code> and methods like <code>.Contains()</code> depend exclusively on the aforementioned interfaces and do not use the operators directly even if they are defined. In fact, when ReSharper generates equality members, it defines both <code>==</code> and <code>!=</code> in terms of <code>Equals()</code> and even then only if the user chooses to generate operators at all. The equality operators aren't needed by the framework to understand object equality.</p> <p>Basically, the .NET framework doesn't care about these operators, it only cares about a few <code>Equals</code> methods. The decision to require both == and != operators to be defined in tandem by the user is related purely to the language design and not object semantics as far as .NET is concerned.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload