2
我發現這個擴展爲C#將GetOrAdd轉換爲Lazy,並且我想爲AddOrUpdate執行相同的操作。ConcurrentDictionary Lazy AddOrUpdate
有人可以幫我將此轉換爲AddOrUpdate?
public static class ConcurrentDictionaryExtensions
{
public static TValue LazyGetOrAdd<TKey, TValue>(
this ConcurrentDictionary<TKey, Lazy<TValue>> dictionary,
TKey key,
Func<TKey, TValue> valueFactory)
{
if (dictionary == null) throw new ArgumentNullException("dictionary");
var result = dictionary.GetOrAdd(key, new Lazy<TValue>(() => valueFactory(key)));
return result.Value;
}
}
有沒有機會向我展示如何使用它的示例? ConcurrentDictionary.AddOrUpdate(CustomerID,1,(k,v)=> v + 1); –
我認爲這可能就是它。字典.LazyAddOrUpdate(1,(ka)=> 1,(k,v)=> v + 1); –
@andre是的你是對的 – xanatos