Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to wrap an object up as a ConcurrentObject?

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);
like image 898
michael Avatar asked Dec 05 '25 03:12

michael


1 Answers

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.

like image 113
Servy Avatar answered Dec 08 '25 05:12

Servy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!