Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Simply speaking, the default comparator compares object instances.</p> <ul> <li><p>In your first example, both <code>_emp1</code> and <code>_emp2</code> are pointing to the same instance of <code>Employee</code> and therefore <code>Equals</code> returns true.</p></li> <li><p>In your second example, you create two <code>Employee</code> objects, <code>_emp1</code> and <code>_emp2</code> being two distinctly different objects. Therefore, <code>Equals</code> returns false.</p></li> </ul> <hr> <p>Note that copy constructors are <em>not implicitly called</em> for reference types. Were you to do:</p> <pre><code>Employee _emp1 = new Employee(); Employee _emp2 = new Employee(_emp1); //or _emp1.Clone() if that is implemented </code></pre> <p>then <code>Equals</code> using a the default comparator will return false since they are two unique objects.</p> <p>Also note that this behavior is <em>not the same</em> for value types.</p> <hr> <p>Furthermore, if you (as you should) override the default behavior of <code>Equals</code> and <code>CompareTo</code>, all the above becomes moot. A standard technique might be (assuming a little bit):</p> <pre><code>public bool Equals(object rhs) { var rhsEmployee = rhs as Employee; if(rhsEmployee == null) return false; return this.Equals(rhsEmployee); } public bool Equals(Employee rhs) { return this.EmployeeId == rhs.EmployeeId; } </code></pre> <hr> <p>Further reading:</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx" rel="nofollow"><code>Object.Equals</code></a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.object.referenceequals.aspx" rel="nofollow"><code>Object.ReferenceEquals</code></a></li> <li><a href="http://msdn.microsoft.com/en-us/library/t63sy5hs%28v=vs.80%29.aspx" rel="nofollow">Value Types and Reference Types</a></li> </ul>
    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. 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.
    3. 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