You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I often use ConcurrentDictionary as a way to force a single instance of a reference typed object.
e.g.
vardict=newConcurrentDictionary<MyClass,MyClass>();varitem1=newMyClass();varresultItem1=dict.GetOrAdd(item1,item1);varitem2=newMyClass();varresultItem2=dict.GetOrAdd(item2,item2);Assert.Same(item1,resultItem2);// Expect reference equality to the first item that was added
I would prefer to use your ConcurrentHashSet, but I need an atomic way to both add and retrieve an item. Otherwise, I have to make two calls - one to add, and one to retrieve, so performance wise I'm better off sticking with ConcurrentDictionary.
Could we add another overload of Add which has an out T parameter to allow the item that is in the set after the Add operation to be returned?