If I have a
Dictionary<int, StreamReader> myDic = new Dictionary<int, StreamReader>
//Populate dictionary
One thread does
myDic[0] = new StreamReader(path);
Another thread does
myDic[1] = new StreamReader(otherpath)
Is this thread safe because the actual item in the dictionary getting modified is different to the one on the other thread or will I get a InvalidOperationException: Collection was modified
You will only get
InvalidOperationException: Collection was modifiedif you enumerate the dictionary while modifying.However, that is not thread-safe.
If one of those operations causes the dictionary to resize, the other one may get lost.
Instead, use
ConcurrentDictionary.