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.

Inspect your APIs for ICollection usage

Inspect your APIs for ICollection usage

- [Instructor] Now we know that ConcurrentDictionary implements ICollection and that we're not supposed to work with the add method or remove method because they're not thread safe. Well, the only way to see those methods is if we explicitly cast. So I would have to write code like this. Now I've got this variable called collection, I take my instance of ConcurrentDictionary, which is called robotGems, and I cast it to ICollection of KeyValuePair, of string int. And now that I've done that, I can go to collection dot and I have the add method and the remove method. So we don't want to do that so I will comment out this line of code so we don't make that mistake and we're done, right? Not really. Think about all of the applications you've worked with. You've got libraries that have helper methods and there's APIs out there that have lots of methods that you use in your applications and a lot of times, those methods take…

Contents