

It's clear that by using delegates we can solve the second aforementioned issue however, still, it is not clear to me how we can address the first issue. The second workaround is to use ConcurrentDictionary. MyDictionary = (myDictionary * 2) + aValue For instance, you can not do a logic similar to: if (myDictionary.ContainsKey(aKey))
#MYDICTIONARY SWAG UPDATE#
Second, the update can not be a function of the old value. For instance, you can not rewrite following code based on this workaround: int addCounter = 0, updateCounter = 0 However, this method has two drawbacks:įirst, you don't know if aKey exist or not that prevents you from additional logics. If aKey exist it's value is replaced with aValue if does not exist, a KeyValuePair with aKey as key and aValue as value is added to myDictionary. I profiled my code, and it shows 80% of CPU time spent on this section (see the following figure), so this motivates an improvement.įirst workaround is simply: myDictionary = aValue However, in my application similar code is executed roughly 500K times.

I guess the performance of this method is "acceptable" when this code is executed only a few times. This code accesses the dictionary two times, once for determining whether aKey exist, another time for updating (if exists) or adding (if does not exist). Suppose the following code: if (myDictionary.ContainsKey(aKey))
