C# set of preknown integers as Dictionary custom key
<p>I want to create a dictionary that is conceptually  <code>Dictionary&lt;HashSet&lt;int&gt;, FooBar&gt;</code> where the <code>HashSet&lt;int&gt;</code> in the key has the following restriction:</p>

<ol>
<li>The members can only be chosen from 0 to N</li>
</ol>

<p>I think with the restriction, there should be a more performant way to do this than writing a custom <code>IEqualityComparer</code> (as described in this post <a href="https://stackoverflow.com/questions/10020541/c-sharp-list-as-dictionary-key">C# List as Dictionary key</a>). For example, when N&lt;64, each of such hash sets can be mapped to a unique Int64. Say the list contains 1,3,18,29, then <code>(1 &lt;&lt; 1) + (1 &lt;&lt; 3) + (1 &lt;&lt; 18) + (1 &lt;&lt; 29)</code>which is 537133066 can represent this combination, so I could implement a <code>Dictionary&lt;Int64, FooBar&gt;</code> to achieve my goal. However apparently this approach doesn't scale beyond 64. </p>

<p>------11/11/2016 Update------</p>

<p>Thanks those who commented below. Now I have better understanding on how hash works, and I think trying to create a collision-free hash for the <code>HashSet&lt;int&gt;</code> as I described with unbounded N is either impossible or too hard and thus not worthwhile. </p>

<p>Also I found a good solution under this question: <a href="https://stackoverflow.com/questions/5910137/how-do-i-use-hashsett-as-a-dictionary-key">How do I use HashSet&lt;T&gt; as a dictionary key?</a>. I am not sure about how its performance is, but at least it is very easy to implement. </p>