ConcurrentDictionary.GetOrAdd and Thread Safety

added by tdupont
12/8/2013 12:24:12 AM

4 Kicks, 110 Views

System.Collections.Concurrent.ConcurrentDictionary.GetOrAdd may invoke the valueFactory multiple times per key. If you are using this Dictionary to store large or expensive objects, then the accidental instantiation of multiple of these objects could be a real problem for your application. There is a very simple solution to avoid this problem: just create Lazy wrappers for your expensive objects.