1

So I have a special class which can remove itself from a collection, let's say it looks something like this:

public class FunTimes{
    public ICollection<FunTimes> Collection {get;set;}
    protected void RemoveFromCollection(){
        Collection.Remove(this);
    }
}

RemoveFromCollection() will be called from an event. I want the collection to add and remove items quickly, and since position doesn't matter in my case, I planned to use something other than a List<T>. I can't really use a ConcurrentBag<T>, since I have to iterate over every item to remove an item. I've finally decided to use a Dictionary<FunTimes, FunTimes>, and then use each FunTimes instance as both the key AND the value. I'm asking if there's a better solution because using an object as both the key and the value in a Dictionary just seems plain weird. Any suggestions?

1 Answers1

6

If Dictionary<FunTimes, FunTimes> satisfies your needs better option would be using HashSet<T> which should have O(1) addition and removal.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132