Search code examples
c#collectionsconcurrent-collections

ConcurrentKeyedCollection approach


Id like to make use of a ConcurrentKeyedCollection - but it doesn't exist.
Why no ConcurrentKeyedCollection?
What should I be using? (ConcurrentDictionary? Wrap my own concurrency protection around a KeyedCollection?)


Solution

  • SynchronizedKeyedCollection introduced in .NET 2.0 does the job - using a List and wrapping access in a lock. Note that the newer "Concurrent" collections introduced in .NET 4.0 used an improved mechanism and should scale better - but they dont offer a KeyedCollection. More info on "Synchronized" vs "Concurrent" collections here.