Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As others already mentioned, when you want to check for reference equality, bypassing any <code>==</code> or <code>Equals()</code>, use <a href="http://msdn.microsoft.com/en-us/library/system.object.referenceequals" rel="nofollow"><code>object.ReferenceEquals()</code></a>.</p> <p>Also, when overloading <code>==</code>, you should also override <code>Equals()</code> (and implement <code>IEquatable&lt;T&gt;</code>). When you do that, you can then take advantage of the fact that <code>object.Equals(object, object)</code> makes all the null checks and then invokes <code>Equals()</code>. So, your code could look like this:</p> <pre><code>public bool Equals(HalfDay other) { if (object.ReferenceEquals(other, null)) return false; if (object.ReferenceEquals(other, this)) return true; return this.Date.Date == other.Date.Date &amp;&amp; this.Half == other.Half; } public int GetHashCode() { // TODO } public override bool Equals(object other) { return Equals(other as HalfDay); } public static bool operator ==(HalfDay halfday1, HalfDay halfday2) { return object.Equals(halfday1, halfday2); } public static bool operator !=(HalfDay halfday1, HalfDay halfday2) { return !(halfday1 == halfday2); } </code></pre> <p>Also, keep in mind what happens when there is a type that inherits from <code>HalfDay</code>. If you don't want to deal with that, make <code>HalfDay</code> <code>sealed</code>.</p> <p>But, considering that you're also implementing comparison operators, you should considering unifying those too. For an example (with detailed explanation), see <a href="http://ericlippert.com/2013/10/07/math-from-scratch-part-six-comparisons/" rel="nofollow">Eric Lippert's <em>Math from scratch, part six: comparisons</em></a> (you don't need to read the rest of the series).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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