From the course: Advanced C#: Thread-Safe Data with Concurrent Collections

Unlock this course with a free trial

Join today to access over 22,500 courses taught by industry experts.

Use the AddOrUpdate method

Use the AddOrUpdate method

- [Instructor] The ConcurrentDictionary has a method that performs one of two actions. It's call AddOrUpdate and its actions is implied in its name. It either adds a new item to the dictionary if it doesn't exist or it updates an item in the dictionary based on its key. Some experts consider this a better choice over TryUpdate. But here's the thing to understand. It's more complex and you are responsible for writing one or more delegates that add or update values in the ConcurrentDictionary. That means you need to think about what does the add method do and what does the update method do. And if you're going to write those methods, it may occur to you that we could write code like I'm showing here on line 45. If the robotGems contains the key, then write the code update the dictionary. Otherwise, add the code add the items to the dictionary. Now the problem with this approach, and there's two problems here. One is that…

Contents