{"id":16,"date":"2007-06-10T12:57:00","date_gmt":"2007-06-10T12:57:00","guid":{"rendered":"http:\/\/localhost:10265\/?p=16"},"modified":"2007-06-10T12:57:00","modified_gmt":"2007-06-10T12:57:00","slug":"gethashcode-does-it-return-unique-values-for-different-inputs","status":"publish","type":"post","link":"https:\/\/mehdi.biz\/blog\/2007\/06\/10\/gethashcode-does-it-return-unique-values-for-different-inputs\/","title":{"rendered":"GetHashCode &#8211; Does it return unique values for different inputs?"},"content":{"rendered":"<!-- google_ad_section_start --><p>The <strong>Object<\/strong> class of the .NET framework has got a virtual method, say, <strong>GetHashCode<\/strong> that other classes could override to provide a custom implementation. This function is suitable for use in hashing algorithms like a hash table. The return value [of the default implementation] is and has never been meant to be used as a <del datetime=\"2011-08-06T19:42:19+00:00\">unique key<\/del>. Although, an ideal implementation would guarantee that, however, it is impossible or impractical.<\/p>\n<p>If you are going to implement your own GetHashCode method, you need to keep the following three criterias in your mind:<\/p>\n<ul>\n<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>\n<li>For the best performance, a hash function must generate a random distribution for all input.<\/li>\n<li>The hash function must return exactly the same value regardless of any changes that are made to the object.<\/li>\n<\/ul>\n<p>Many people believe (unfortunately) that the default implementation of String.GetHashCode function returns a unique number for different input values. However, Mashiharu&#8217;s sample states that the two following different string, produces the same hash code [on .NET FX 2.0]:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nTrace.WriteLine(&quot;0:93121&quot;.GetHashCode());\nTrace.WriteLine(&quot;0:2546870&quot;.GetHashCode());\n<\/pre>\n<p>Having this in mind, the GetHashCode function could be implemented by XORing the field values with each other:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic struct MyStruct1\n{\n    public int field1;\n    public int field2;\n\n    public override int GetHashCode()\n    {\n        return field1 ^ field2;\n    }\n}\n<\/pre>\n<p>Another common approach is to use a prime#:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic struct MyStruct2\n{\n    public int field1;\n    public int field2;\n\n    public override int GetHashCode()\n    {\n        return 19 * field1.GetHashCode() + 1 * field2.GetHashCode();\n    }\n}\n<\/pre>\n<p>where 19 is a prime#.<\/p>\n<p>There are more complicated approaches each of which deserve a separate post. Whatever your approach is, just follow those above-mentioned rules. For more information, and some implementations of the GetHashCode, checkout the following sites:<\/p>\n<ol>\n<li><a href=\"http:\/\/msdn2.microsoft.com\/en-us\/default.aspx\">Microsoft Developer Network (MSDN)<\/a><\/li>\n<li>Frank Hileman&#8217;s notes on hash functions<\/li>\n<li><a href=\"http:\/\/www.interact-sw.co.uk\/iangblog\/2004\/06\/21\/gethashcode\">Ian Griffiths<\/a>, <a href=\"http:\/\/www.azillionmonkeys.com\/qed\/hash.html\">Paul Hsieh<\/a> &#038; <a href=\"http:\/\/www.mashiharu.com\/\">Mashiharu<\/a>.<\/li>\n<li><a href=\"http:\/\/en.wikipedia.org\/wiki\/Hash_function\">Wikipedia<\/a><\/li>\n<\/ol>\n<!-- google_ad_section_end -->","protected":false},"excerpt":{"rendered":"<p>The Object class of the .NET framework has got a virtual method, say, GetHashCode that other classes could override to provide a custom implementation. This function is suitable for use in hashing algorithms like a hash table. The return value [of the default implementation] is and has never been meant to be used as a [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":0,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"wp:attachment":[{"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mehdi.biz\/blog\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}