<p>The MSDN documentation on <a href="http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx" rel="noreferrer">Object.GetHashCode()</a> describes 3 contradicting rules for how the method should work.  </p>

<ol>
<li>If two objects of the same type represent the same value, the hash function must return the same constant value for either object.</li>
<li>For the best performance, a hash function must generate a random distribution for all input.</li>
<li>The hash function must return exactly the same value regardless of any changes that are made to the object.</li>
</ol>

<p>Rules 1 &amp; 3 are contradictory to me.</p>

<p>Does Object.GetHashCode() return a unique number based on the <strong>value</strong> of an object, or the <strong>reference</strong> to the object. If I override the method I can choose what to use, but I'd like to know what is used internally if anyone knows.</p>

[{'answer_id': 34511, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 0, 'selected': False, 'text': '<p>By default it does it based on the reference to the object, but that means that it\'s the exact same object, so both would return the same hash. But a hash should be based on the value, like in the case of the string class. "a" and "b" would have a different hash, but "a" and "a" would return the same hash.</p>\n'}, {'answer_id': 34541, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 6, 'selected': True, 'text': "<blockquote>\n  <p>Rules 1 &amp; 3 are contradictory to me.</p>\n</blockquote>\n\n<p>To a certain extent, they are. The reason is simple: if an object is stored in a hash table and, by changing its value, you change its hash then the hash table has lost the value and you can't find it again by querying the hash table. It is important that while objects are stored in a hash table, they retain their hash value.</p>\n\n<p>To realize this it is often simplest to make hashable objects immutable, thus evading the whole problem. It is however sufficient to make only those fields immutable that determine the hash value.</p>\n\n<p>Consider the following example:</p>\n\n<pre><code>struct Person {\n    public readonly string FirstName;\n    public readonly string Name;\n    public readonly DateTime Birthday;\n\n    public int ShoeSize;\n}\n</code></pre>\n\n<p>People rarely change their birthday and most people never change their name (except when marrying). However, their shoe size may grow arbitrarily, or even shrink. It is therefore reasonable to identify people using their birthday and name but not their shoe size. The hash value should reflect this:</p>\n\n<pre><code>public int GetHashCode() {\n    return FirstName.GetHashCode() ^ Name.GetHashCode() ^ Birthday.GetHashCode();\n}\n</code></pre>\n"}, {'answer_id': 34566, 'author': 'Scott Dorman', 'author_id': 1559, 'author_profile': 'https://Stackoverflow.com/users/1559', 'pm_score': 3, 'selected': False, 'text': '<p>Not sure what MSDN documentation you are referring to. Looking at the current documentation on Object.GetHashCode (<a href="http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx</a>) provides the following "rules":</p>\n\n<ul>\n<li><p>If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values. </p></li>\n<li><p>The GetHashCode 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. </p></li>\n<li><p>For the best performance, a hash function must generate a random distribution for all input. </p></li>\n</ul>\n\n<p>If you are referring to the second bullet point, the key phrases here are "as long as there is no modification to the object state" and "true only for the current execution of an application". </p>\n\n<p>Also from the documentation, </p>\n\n<blockquote>\n  <p>A hash function is used to quickly generate a number (hash code) that corresponds to the <strong>value</strong> of an object. Hash functions are usually specific to each Type and must use at least one of the <strong>instance</strong> fields as input. [<em>Emphasis added is mine.</em>]</p>\n</blockquote>\n\n<p>As for the actual implementation, it clearly states that derived classes can defer to the Object.GetHashCode implementation <strong>if and only if</strong> that derived class defines value equality to be reference equality and the type is not a value type. In other words, the default implementation of Object.GetHashCode is going to be based on reference equality since there are no real instance fields to use and, therefore, does not guarantee unique return values for different objects. Otherwise, your implementation should be specific to your type and should use at least one of your instance fields. As an example, the implementation of String.GetHashCode returns identical hash codes for identical string values, so two String objects return the same hash code if they represent the same string value, and uses all the characters in the string to generate that hash value.</p>\n'}, {'answer_id': 34573, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 2, 'selected': False, 'text': "<p>Rules 1 &amp; 3 aren't really a contradiction.</p>\n\n<p>For a reference type the hash code is derived from a reference to the object - change an object's property and the reference is the same.</p>\n\n<p>For value types the hash code is derived from the value, change a property of a value type and you get a completely new instance of the value type.</p>\n"}, {'answer_id': 337351, 'author': 'Ilya Ryzhenkov', 'author_id': 18575, 'author_profile': 'https://Stackoverflow.com/users/18575', 'pm_score': 0, 'selected': False, 'text': "<p>I can't know for sure how Object.GetHashCode is implemented in <em>real</em> .NET Framework, but in Rotor it uses SyncBlock index for the object as hashcode. There are some blog posts about it on the web, however most of them are from 2005. </p>\n"}, {'answer_id': 32736203, 'author': 'thewhiteambit', 'author_id': 2042691, 'author_profile': 'https://Stackoverflow.com/users/2042691', 'pm_score': 1, 'selected': False, 'text': '<p>A very good explanation on how to handle <code>GetHashCode</code> (beyond Microsoft rules) is given in Eric Lipperts (co. Designer of C#) Blog with the article "<a href="http://ericlippert.com/2011/02/28/guidelines-and-rules-for-gethashcode/" rel="nofollow">Guidelines and rules for GetHashCode</a>". It is not good practice to add hyperlinks here (since they can get invalid) but this one is worth it, and provided the information above one will probably still find it in case the hyperlink is lost.</p>\n'}]
['https://Stackoverflow.com/questions/34505', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/392/']