A subtle concurrency trap in ConcurrentDictionary<TKey, TValue>:
GetOrAdd(key, valueFactory) guarantees that only one single instance is stored in the dictionary. However, under high thread contention, the valueFactory delegate may be invoked multiple times concurrently!
If your factory opens a database connection or creates expensive resources, use Lazy<T>:
var item = dict.GetOrAdd(key, k => new Lazy<ExpensiveResource>(() => new ExpensiveResource(k))).Value;Lazy<T> guarantees that the constructor executes exactly once across all competing threads!