如果该值是一个集合,如何AddorUpdate
在 ConcurrentDictionary 中实现以便我可以正确更新该值?
我担心的是,由于 TValue 是一种引用类型,我可能会遇到在竞争条件下多次调用 TValue 的情况。我自己会对此进行测试,但我的语法错误,所以我无法继续进行。
我必须改变什么才能使这项工作?
public class TrustList : ConcurrentDictionary<int, List<TrustRelationshipDetail>>
{
public void AddOrUpdateTrustDetail(TrustRelationshipDetail detail)
{
List<TrustRelationshipDetail> detailList = new List<TrustRelationshipDetail>();
detailList.Add(detail);
this.AddOrUpdate(detail.HierarchyDepth, detailList, (key, oldValue) =>
oldValue.Add(detail) // <--- Compiler doesn't like this, and I think this may cause duplicates if this were to be called...
);
}
}