.NET port with Java's Map, Set, HashMap
<p>I am porting Java code in .NET and I am stuck in the following lines that (behave unexpectedly in .NET).</p>

<p><strong>Java</strong>:</p>

<pre><code>Map&lt;Set&lt;State&gt;, Set&lt;State&gt;&gt; sets = new HashMap&lt;Set&lt;State&gt;, Set&lt;State&gt;&gt;();

Set&lt;State&gt; p = new HashSet&lt;State&gt;();

if (!sets.containsKey(p)) { ... }
</code></pre>

<p>The equivalent <strong>.NET</strong> code could possibly be:</p>

<pre><code>IDictionary&lt;HashSet&lt;State&gt;, HashSet&lt;State&gt;&gt; sets = new Dictionary&lt;HashSet&lt;State&gt;, HashSet&lt;State&gt;&gt;();

HashSet&lt;State&gt; p = new HashSet&lt;State&gt;();

 if (!sets.containsKey(p)) { /* (Add to a list). Always get here in .NET (??) */ }
</code></pre>

<p>However the code comparison fails, the program think that "sets" never contain Key "p" and eventually results in OutOfMemoryException.</p>

<p>Perhaps I am missing something, object equality and identity might be different between Java and .NET.</p>

<p><em>I tried implementing IComparable and IEquatable in class State but the results were the same.</em></p>

<p><strong>Edit:</strong> </p>

<p>What the code does is: If the sets does not contain key "p" (which is a HashSet) it is going to add "p" at the end of a LinkedList>.</p>

<p>The State class (Java) is a simple class defined as:</p>

<pre><code>public class State implements Comparable&lt;State&gt; {

boolean accept;
Set&lt;Transition&gt; transitions;

int number;
int id;

// ...

public int compareTo(State s) {
    return s.id - id;
}

public boolean equals(Object obj) {
    return super.equals(obj);
}

public int hashCode() {
    return super.hashCode();
}
</code></pre>
