How do you set a value in a ConcurrentDictionary regardless of whether it contains a key

First of all, is it safe to just add an element to a parallel dictionary using an indexed destination (e.g. myConcurrentDictionary[someKey] = someValue;)?

I'm just confused because it hides IDictionary methods (e.g., Add).

Why is the func function required to add the value of the AddOrUpdate () parameter? Is there a way to set a value for a key regardless of whether the key exists?

I could not compile this on the MSDN page.

+4
source share
2 answers
  • Yes, using an indexer should be absolutely perfect if you want to add or replace a value.

  • AddOrUpdate , "" "" , , . , .

  • - " , ", IDictionary<,>.

- - :

...
/


: dictionary[key] = newValue

, .

+11

, , , , , . "" . , , .

Add , TryAdd. , . , , . , , .

AddOrUpdated :

  • .
  • , . , , .
+2

Source: https://habr.com/ru/post/1525472/


All Articles