Let say I have:
MyCollection = new ConcurrentDictionary<string, int>();
I now it is safe to add and remove items from MyCollection
. But What about modifying items. For example is it safe to do:
MyCollection["key"] = 1; // thread 1
and
MyCollection["key"] = 2; // thread 2
Example 2:
MyCollection2 = new ConcurrentDictionary<string, List<int>>();
Is it safe to do?
MyCollection2["key"].Add(1); // Thread1
and
MyCollection2["key"].Add(2); // Thread2
where Thread1 and Thread2 are executing at the same time. Do I have to create a lock when modifying items?