I have KeyValuePairs and i want to add the new value to a ConcurrentDictonary. If the key is not contained i want to add it. But what is faster/better:
this:
dict.AddOrUpdate(pair.Key, pair.Value, (ok, ov) => pair.Value);
or this:
if (dict.ContainsKey(pair.Key))
{
dict[pair.Key] = pair.Value;
}
else
{
dict.TryAdd(pair.Key, pair.Value);
}
I am concerned that the AddOrUpdate does additional work that i don't want/need and that it takes longer because the Lambda has to be executed too.
Which of those two methods is faster? Or is there a even faster Method?