Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3805578
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:42:01+00:00 2026-05-19T14:42:01+00:00

(edit) more info. First notice new virtual. This class inherits a base class which

  • 0

(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:

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

This class:

 public class ParentAwareHashset<TObject, TParent> : 
   ParentAwareCollection<TObject, TParent, HashSet<TObject>>, ISet<TObject>, 
   ICollection  where TObject : IParentProvider<TParent>
    {
        public ParentAwareHashset(TParent parent)
            : base(parent)

IParentProvider just requires that a “ParentCollection” object be present.

In debugging, the overridden (void) Add method in ParentAwareCollection is never called. So I don’t think that the “new” is the problem.

Also, here’s something that I also don’t understand. Here’s the descriptor for an actual HashSet:

public class HashSet<T> : ISerializable, IDeserializationCallback, 
ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable
{
...
        public bool Add(T item);
}

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 new descriptor for its Add() method, but that’s required because ICollection<T> implements void Add(T item). Perhaps they just omitted it?

Original question:

I have a collection class that uses a HashSet<T> to store its objects. The objects of type T override GetHashCode(), 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.

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:

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

    if (InnerList.Any(existing=>item.GetHashCode()==existing.GetHashCode())) {
        return(false);
    } else {
        if (InnerList.Add(item))
        {
            return(true);
        } else {
            return(false);
        }
    }
}

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?

Here’s what I observed while debugging:

Breaking before InnerList.Add():

?InnerList.Count
1
?item.GetHashCode()
-1629834529
?InnerList.ElementAt(0).GetHashCode()
-1629834529

Step over the InnerList.Add(), which returns true:

?InnerList.Count
1
?InnerList.Add(item)
false

Wtf? This is .net 4.0 framework.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:42:02+00:00Added an answer on May 19, 2026 at 2:42 pm

    I figure it out. This is a very insidious little thing. So the whole point of this setup is so that I can have collection classes that have a “Parent”, and when you add items to the collection, they are automatically assigned that Parent. (This Parent is not simply the class itself, it’s another object).

    Here is the problem:

            CsmResourceHashSet resources = new CsmResourceHashSet(Context);
            resources.AddFrom(Context.ScriptResources
                .Where(item=>item.Enabled));
    

    CsmResourceHashSet is a ParentAwareHashset. Extension method AddFrom:

    public static void AddFrom<T>(this ICollection<T> destList, IEnumerable<T> sourceList)
    {
        foreach (T obj in sourceList)
        {
            destList.Add(obj);
        }
    }
    

    so it’s apparently using the ICollection implementation to perform the Add. The void Add method of the base ParentAwareCollection is being used.

    I guess it is casting my object to the base class when it uses this, even though I have a ‘new Add. It seems weird to me that when you override a method with new, that an instance of that object could still have it’s base method operated on externally.

    @Anthony Pegram’s comment actually enabled me to find the solution. So what was happening is that the void Add() was being called, which consequently called the actual derived InnerList Add(), which was indeed returning false, but that value was never getting back to code that needed to know about it.

    The effect was that the HashSet actually had the correct contents, but Parent didn’t get set for the one that got rejected from the HashSet. So this made the code work almost all the time… except when it mattered that the code didn’t know something had been rejected. But without the “Equals” check, also, I was permitting duplicates of things that I shouldn’t have been.

    Solution was to add another ExtensionMethod for ISet<>

    public static void AddFrom<T>(this ISet<T> destList, IEnumerable<T> sourceList)
    {
        foreach (T obj in sourceList)
        {
            destList.Add(obj);
        }
    }
    

    Once I did that, it works. Well, actually it didn’t work, but it revealed a couple crazy little bugs that mostly didn’t manifest themselves, but all my exceptions started blowing up everywhere and it took no time to fix them all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: This question is more about language engineering than C++ itself. I used C++
EDIT: I've tagged this C in a hope to get more response. It's more
Edit: This question was written in 2008, which was like 3 internet ages ago.
--Edit with more bgnd information-- A (black box) COM object returns me a string.
EDIT: I'm still waiting for more answers. Thanks! In SQL 2000 days, I used
EDIT: For the inner queries, there could be more than one match per inner
Please note the Edit below for a lot more information, and a possible solution
First of all, im new here, and im (not) a pro ;) (but i
Edit: From another question I provided an answer that has links to a lot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.