1

ConcurrentDictionary offer constant read time. I do not need key-value pairs, only keys.. I searched for read times on ConcurrentBag and havent found how it is implemented?

Is there a constant read time ConcurrentCollection, besides ConcurrentDictionary?

Robert Segdewick
  • 543
  • 5
  • 17
  • Also look at [immutable](https://learn.microsoft.com/en-us/dotnet/api/system.collections.immutable?view=netcore-3.1) collections. E.g. `ImmutableHashSet` – Alexander Petrov Sep 26 '20 at 14:58

1 Answers1

1

ConcurrentBag is probably not what you are looking for:

Represents a thread-safe, unordered collection of objects.

Which means that it allows duplicates (whereas the dictionary doesn't)

Bags are useful for storing objects when ordering doesn't matter, and unlike sets, bags support duplicates.

As for performance, it certainly isn't as much performant as a list (so at least O(n)) (C# - Performance comparison of ConcurrentBag vs List)

For a ConcurrentSet check your luck with the custom implementation here: How to implement ConcurrentHashSet in .Net

You can also check the list of Concurrent collections to see if something else suits your needs.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61