Concurrent Dictionary Correct Usage

前端 未结 5 937
名媛妹妹
名媛妹妹 2020-12-13 05:27

Am I right in thinking this is the correct use of a Concurrent Dictionary

private ConcurrentDictionary myDic = new ConcurrentDictionary

        
5条回答
  •  再見小時候
    2020-12-13 05:51

    It depends, in my case I prefer using this method.

    ConcurrentDictionary.AddOrUpdate Method (TKey, Func, Func);
    

    See MSDN Library for method usage details.

    Sample usage:

    results.AddOrUpdate(
      Id,
      id => new DbResult() {
         Id = id,
         Value = row.Value,
         Rank = 1
      },
      (id, v) =>
      {
         v.Rank++;
         return v;
      });
    

提交回复
热议问题