Hashset not adding a duplicate, but returning true for Add()
<p>(edit) more info. First notice "new virtual". This class inherits a base class which is supposed to be a generic parent-aware class that can be created with any ICollection type. Here's the descriptor, basically:</p>

<pre><code>public abstract class ParentAwareCollection&lt;TObject, TParent, TInnerList&gt; :
 ICollection&lt;TObject&gt;, ICollection, IParentProvider&lt;TParent&gt;
    where TObject : IParentProvider&lt;TParent&gt;
    where TInnerList : ICollection&lt;TObject&gt;, new()
{
        protected TInnerList InnerList = new TInnerList();
...
}
</code></pre>

<p>This class:</p>

<pre><code> public class ParentAwareHashset&lt;TObject, TParent&gt; : 
   ParentAwareCollection&lt;TObject, TParent, HashSet&lt;TObject&gt;&gt;, ISet&lt;TObject&gt;, 
   ICollection  where TObject : IParentProvider&lt;TParent&gt;
    {
        public ParentAwareHashset(TParent parent)
            : base(parent)
</code></pre>

<p>IParentProvider just requires that a "ParentCollection" object be present.</p>

<p>In debugging, the overridden (void) Add method in <code>ParentAwareCollection</code> is never called. So I don't think that the "new" is the problem.</p>

<p>Also, here's something that I also don't understand. Here's the descriptor for an actual HashSet:</p>

<pre><code>public class HashSet&lt;T&gt; : ISerializable, IDeserializationCallback, 
ISet&lt;T&gt;, ICollection&lt;T&gt;, IEnumerable&lt;T&gt;, IEnumerable
{
...
        public bool Add(T item);
}
</code></pre>

<p>Notice that in implements ICollection just like mine. That is what led me to believe that this was OK to do. However unlike mine, the framework HashSet does not use a <code>new</code> descriptor for its Add() method, but that's required because <code>ICollection&lt;T&gt;</code> implements <code>void Add(T item)</code>. Perhaps they just omitted it?</p>

<p>Original question:</p>

<p>I have a collection class that uses a <code>HashSet&lt;T&gt;</code> to store its objects. The objects of type T override <code>GetHashCode()</code>, in which they make sure that certain required information is present that is used to produce the hash code. I am not sure if this is important.</p>

<p>What happens is that when I do an Add(T) to the HashSet, it does not add the object, but returns true. If I debug and then in the immediate window try to add it again, it returns false, like it's supposed to. The method looks like this:</p>

<pre><code>public new virtual bool Add(TObject item)
{
    // Must add parent first, since it may be used in the hash code
    // InnerList is a HashSet&lt;T&gt;

    if (InnerList.Any(existing=&gt;item.GetHashCode()==existing.GetHashCode())) {
        return(false);
    } else {
        if (InnerList.Add(item))
        {
            return(true);
        } else {
            return(false);
        }
    }
}
</code></pre>

<p>I added the first condition to make my code actually work. It works as expected with this there. However, I can't understand why I would have to do this, and I can think of no reason, ever, why a HashSet would return "true" for Add() without adding anything. Even if my GetHashCode override is messed up, it should either add it and return true, or not and return false. Any thoughts?</p>

<p>Here's what I observed while debugging:</p>

<p>Breaking before InnerList.Add():</p>

<pre><code>?InnerList.Count
1
?item.GetHashCode()
-1629834529
?InnerList.ElementAt(0).GetHashCode()
-1629834529
</code></pre>

<p>Step over the InnerList.Add(), which returns true:</p>

<pre><code>?InnerList.Count
1
?InnerList.Add(item)
false
</code></pre>

<p>Wtf? This is .net 4.0 framework.</p>
