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.

Why ICollection and other interfaces are not thread-safe

Why ICollection and other interfaces are not thread-safe - C# Tutorial

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

Why ICollection and other interfaces are not thread-safe

- [Instructor] You've heard me say many times in this course that the members, the methods on ConcurrentDictionary are thread safe. But let's dig into this a little bit deeper. Here's what it says in the Microsoft documentation on line 25. "All public and protected members "of ConcurrentDictionary are thread-safe." Now here's the caveat on line 26. "However, members accessed through "one of the interfaces it implements "are not guaranteed to be thread-safe "and may need to be synchronized by the caller." So what this means is the members that were written specifically for ConcurrentDictionary are thread-safe, but the ones coming from the interfaces are not. And there's a lot of interfaces, I'm showing them here. ICollection, there's two IEnumberables, the generic and non-generic version. There's IDictionary, IReadOnlyCollection, and IReadOnlyDictionary. So let's take a look at this one, ICollection. So we'll start by…

Contents