.NET中是否有一种无序集合可以快速添加和删除对象?

oct*_*ker 1 .net c# data-structures

所以我有一个特殊的类,它可以从集合中删除自己,假设它看起来像这样:

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

RemoveFromCollection()将从事件中调用。我希望集合能够快速添加和删除项目,并且由于位置在我的情况下并不重要,因此我计划使用List<T>. 我不能真正使用 a ConcurrentBag<T>,因为我必须迭代每个项目才能删除项目。我最终决定使用 a Dictionary<FunTimes, FunTimes>,然后使用每个FunTimes实例作为键值。我问是否有更好的解决方案,因为使用对象作为字典中的键和值似乎很奇怪。有什么建议么?

Gur*_*ron 6

如果Dictionary<FunTimes, FunTimes>满足您的需求,更好的选择是使用HashSet<T>应该有O(1) 的添加和删除