I know how to utilize the ConcurrentDictionary<TKey, TValue>
class by using a GetOrAdd()
method very nicely. Is there a similar class for a single object instead? I'd rather not create a dictionary for a single object just to get concurrency.
Right now, I'm doing the following:
return _singledict.GetOrAdd(_sync, CreateSingleItem);
It sounds like you want to have some means of accessing an existing object if it has already been constructed, and constructing a new one if it hasn't been, with the appropriate tools in place to prevent the construction of multiple objects.
This is exactly what Lazy<T>
exists for.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments