title: Fast string comparison with list
body: <p>I need a fast method to determine if a given string is in a list of strings.</p>
  
  <p>The list of strings is not known until runtime but thereafter it will not change.</p>
  
  <p>I could simply have a <code>List&lt;String&gt;</code> called <code>strings</code> and then do:</p>
  
  <pre><code>if (strings.Contains(item))
  </code></pre>
  
  <p>However this will perform poorly if there are many strings in the list.</p>
  
  <p>I could also use a <code>HashSet&lt;String&gt;</code>, but this would necessitate calling <code>GetHashCode</code> on every incoming string as well as <code>Equals</code>, which would be a waste if there are e.g. only 3 strings in the list. Did I mention this needs to be <b>fast</b>?</p>
  
  <p>I could when setting up, decide to use a <code>List</code> or a <code>HashSet</code> depending on the number of strings (e.g. use List for  less than 10 strings, HashSet otherwise), rather like the logic in <code>HybridDictionary</code>.</p>
  
  <p>As the strings are unicode, a standard Trie structure will not work, although a Radix tree/Patricia trie might. Are there any good C# implementations out there with benchmarks?</p>
  
  <p>Some have mentioned bypassing <code>String</code>'s <code>GetHashCode</code> and using a faster performing hash function. Are there any benchmarks out there?</p>
  
  <p>Using LINQ expressions to essentially create an optimised switch statement is a novel approach which looks very interesting.</p>
  
  <p>What else would work? Setup cost is not important, just the search speed.</p>
  
  <p>If it matters, the incoming string values will rarely appear in the list.</p>