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

Unlock this course with a free trial

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

Use TryUpdate to update dictionary value

Use TryUpdate to update dictionary value - C# Tutorial

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

Use TryUpdate to update dictionary value

- [Instructor] The first attempt at updating with ConcurrentDictionary is to use a method called TryUpdate. Now, I've left the old example in here. It's up here in this region, wrong way to update, and I've added some new code to work with robot number four this time. So let's start with taking a look at what we're doing here. Let's say that we go out and search for gems and for robot four we find two, two new gems, then I output that we found two gems for the robot. Then I get the real value here. Now, robot number four has 40 gems in his gem count. So the total after this whole operation is done is 42. Then I add together the current gem count 40 plus the found count of two. At this point, I'm ready to call TryUpdate. I'm using name parameters here because I think that really helps clarify what we're doing when we call a method. So the first value is the key, the second value is the new value, and the third value is…

Contents