Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The default implementation of the GetHashCode method uses the reference of the object rather than the values in the object. You have to use the same instance of the object as you used to put the data in the dictionary for that to work.</p> <p>An implementation of <code>GetHashCode</code> that works simply creates a code from the hash codes of it's data members:</p> <pre><code>public int GetHashCode() { return LowerLimit.GetHashCode() ^ UpperLimit.GetHashCode(); } </code></pre> <p>(This is the same implementation that the Point structure uses.)</p> <p>Any implementation of the method that always returns the same hash code for any given parameter values works when used in a Dictionary. Just returning the same hash code for all values actually also works, but then the performance of the Dictionary gets bad (looking up a key becomes an O(n) operation instead of an O(1) operation. To give the best performance, the method should distribute the hash codes evenly within the range.</p> <p>If your data is strongly biased, the above implementation might not give the best performance. If you for example have a lot of ranges where the lower and upper limits are the same, they will all get the hash code zero. In that case something like this might work better:</p> <pre><code>public int GetHashCode() { return (LowerLimit.GetHashCode() * 251) ^ UpperLimit.GetHashCode(); } </code></pre> <p>You should consider making the class immutable, i.e. make it's properties read-only and only setting them in the constructor. If you change the properties of an object while it's in a Dictionary, it's hash code will change and you will not be able to access the object any more.</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