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

Unlock this course with a free trial

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

The GetOrAdd method to get or add an item

The GetOrAdd method to get or add an item - C# Tutorial

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

The GetOrAdd method to get or add an item

- [Instructor] The four operations that we want to perform with a dictionary are the classic CRUD operations. Create, ready, update, and delete. Now for the Create process, we've seen the TryAdd method. For the Delete, we use TryRemove. What we'll look at in the next couple of videos are updating and working with data, or reading the values. And we'll also look at how to combine those with some of the methods that are available on the ConcurrentDictionary. To get an item from the dictionary, there's a couple ways we can do it. Here's probably the simplest way. On line 85, I have the Index Value, or I should say I have the Key, and I'm using it as the Index here. So this reaches into the dictionary, and retrieves a reference to the robot with that key value. Next we're going to look at the GetOrAdd method. So this is an another way of getting it, but it also is a combined operation. It will get the value, but…

Contents