Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is "Best Practice" For Comparing Two Instances of a Reference Type?
    text
    copied!<p>I came across this recently, up until now I have been happily overriding the equality operator (<strong>==</strong>) and/or <strong>Equals</strong> method in order to see if two references types actually contained the same <strong>data</strong> (i.e. two different instances that look the same).</p> <p>I have been using this even more since I have been getting more in to automated testing (comparing reference/expected data against that returned).</p> <p>While looking over some of the <a href="http://msdn.microsoft.com/en-us/library/ms229042(VS.80).aspx" rel="noreferrer">coding standards guidelines in MSDN</a> I came across an <a href="http://msdn.microsoft.com/en-us/library/7h9bszxx(VS.80).aspx" rel="noreferrer">article</a> that advises against it. Now I understand <em>why</em> the article is saying this (because they are not the same <em>instance</em>) but it does not answer the question:</p> <ol> <li><strong>What is the best way to compare two reference types?</strong></li> <li>Should we implement <a href="http://msdn.microsoft.com/en-us/library/system.icomparable.aspx" rel="noreferrer">IComparable</a>? (I have also seen mention that this should be reserved for value types only).</li> <li>Is there some interface I don't know about?</li> <li>Should we just roll our own?!</li> </ol> <p>Many Thanks ^_^</p> <h2>Update</h2> <p>Looks like I had mis-read some of the documentation (it's been a long day) and overriding <a href="http://msdn.microsoft.com/en-us/library/336aedhh(VS.71).aspx" rel="noreferrer">Equals</a> may be the way to go..</p> <blockquote> <p>If you are implementing reference types, you should consider overriding the Equals method on a reference type if your type looks like a base type such as a Point, String, BigNumber, and so on. Most reference types should not overload the <strong>equality</strong> operator, even <strong>if they override Equals</strong>. However, if you are implementing a reference type that is intended to have value semantics, such as a complex number type, you should override the equality operator.</p> </blockquote>
 

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