I have a concurrent dictionary _dict
of type <byte[], int>
I loop through the items of another list of strings (list1
), and if the item exists as a key in _dict
, I increment its value; if it doesn't, I add it to _dict
and set its value to 1
foreach(byte[] item in list1)
{
_dict.AddOrUpdate(item, 1, (key, oldValue) => oldValue + 1);
}
The problem is, even if the item already exists in _dict
, it adds it again to _dict
instead of updating it.
What's wrong with my code?
Updated code block
internal class someClass
{
public List<byte[]> list1 { get; set; }
public ConcurrentDictionary<byte[], int> dict1 { get; set; }
}
class Program
{
static void Main(string[] args)
{
someClass obj = new someClass();
if (obj.list1 != null && obj.list1.Count > 0)
{
foreach (byte[] item in obj.list1)
{
// upsert operation (add if string not in dictionary; update by incrementing count if it does exist)
obj.dict1.AddOrUpdate(item, 1, (key, oldValue) => oldValue + 1);
}
}
}
}