title: How do I calculate a good hash code for a list of strings?
body: <p>Background:</p>
  
  <ul>
  <li>I have a short list of strings.</li>
  <li>The number of strings is not always the same, but are nearly always of the order of a “handful”</li>
  <li>In our database will store these strings in a 2nd normalised table</li>
  <li>These strings are <strong>never</strong> changed once they are written to the database.</li>
  </ul>
  
  <p>We wish to be able to match on these strings quickly in a query without the performance hit of doing lots of joins.</p>
  
  <p>So I am thinking of storing a hash code of all these strings in the main table and including it in our index, so the joins are only processed by the database when the hash code matches.</p>
  
  <p>So how do I get a good hashcode?  I could:</p>
  
  <ul>
  <li>Xor the hash codes of all the string together</li>
  <li>Xor with multiply the result after each string (say by 31)</li>
  <li>Cat all the string together then get the hashcode</li>
  <li>Some other way</li>
  </ul>
  
  <p>So what do people think?</p>
  
  <hr>
  
  <p>In the end I just concatenate the strings and compute the hashcode for the concatenation, as it is simple and worked well enough.</p>
  
  <p><em>(If you care we are using .NET and SqlServer)</em></p>
  
  <hr>
  
  <p><strong>Bug!, Bug!</strong></p>
  
  <p><a href="http://ericlippert.com/2011/02/28/guidelines-and-rules-for-gethashcode/" rel="noreferrer">Quoting from Guidelines and rules for GetHashCode</a> by Eric Lippert </p>
  
  <blockquote>
    <p>The documentation for
    System.String.GetHashCode notes
    specifically that two identical
    strings can have different hash codes
    in different versions of the CLR, and
    in fact they do. Don't store string
    hashes in databases and expect them to
    be the same forever, because they
    won't be.</p>
  </blockquote>
  
  <p>So String.GetHashcode() should not be used for this.</p>