Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In addition to overloading <code>==</code> and <code>Equals</code>, make sure you override <a href="http://msdn.microsoft.com/en-us/library/system.object.gethashcode%28v=vs.110%29.aspx" rel="nofollow noreferrer"><code>GetHashCode</code></a> with a suitable hash function. In particular, see this specification from the documentation:</p> <blockquote> <ul> <li>If two objects compare as equal, the <code>GetHashCode</code> method for each object must return the same value. However, if two objects do not compare as equal, the <code>GetHashCode</code> methods for the two objects do not have to return different values.</li> <li>The <code>GetHashCode</code> method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object's Equals method. Note that this is true only for the current execution of an application, and that a different hash code can be returned if the application is run again.</li> <li>For the best performance, a hash function should generate an even distribution for all input, including input that is heavily clustered. An implication is that small modifications to object state should result in large modifications to the resulting hash code for best hash table performance.</li> <li>Hash functions should be inexpensive to compute.</li> <li>The <code>GetHashCode</code> method should not throw exceptions.</li> </ul> </blockquote> <hr> <p>I agree with <a href="https://stackoverflow.com/a/18341416/1715579"><em>Jon Skeet</em>'s suspicion</a> that you're somehow unintentionally modifying <code>UserID</code> property after it's added as a key. But since the only property that's important for testing equality in <code>MyObject</code> is <code>UserID</code> (and therefore that's the only property that the <code>Dictionary</code> cares about), I'd recommend refactoring your code to use a simple <code>Dictionary&lt;ulong, IMyInterface&gt;</code> instead:</p> <pre><code>Dictionary&lt;ulong, IMyInterface&gt; dict = new Dictionary&lt;string, IMyInterface&gt;(); ulong userID = dict.Keys.First(); var value = dict[userID]; </code></pre>
    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. 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