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
实例作为键和值。我问是否有更好的解决方案,因为使用对象作为字典中的键和值似乎很奇怪。有什么建议么?