This article has a link to this code sample. In this sample, existingVal is changed on the existing object and returned. In this manner, existingVal.lastQueryDate and existingVal.RecentHighTemperatures may be inconsistent if another thread is modifying it at the same time. An solution is to return an new object, then AddOrUpdate can handle the change correctly.
This article has a link to this code sample. In this sample,
existingVal
is changed on the existing object and returned. In this manner,existingVal.lastQueryDate
andexistingVal.RecentHighTemperatures
may be inconsistent if another thread is modifying it at the same time. An solution is to return an new object, thenAddOrUpdate
can handle the change correctly.Source code: https://referencesource.microsoft.com/#mscorlib/system/Collections/Concurrent/ConcurrentDictionary.cs,1151