.NET has 4 built-in dictionary/map types:
- Hashtable
- Dictionary
- ConcurrentDictionary
- ImmutableDictionary
There’s no guidance on when to use what, mostly individual documentation on each implementation.
#dotnet
ConcurrentDictionary - Good read speed even in the face of concurrency, but it’s a heavyweight object to create and slower to update.
Dictionary with lock - Poor read speed lightweight to create and “medium” update speed.
Dictionary as immutable object - best read speed and lightweight to create but heavy update. Copy and modify on mutation e.g. new Dictionary(old).Add(key, value)
Hastable - Good read speed (no lock required), sameish weight as dictionary but more expensive to mutate and no generics!
ImmutableDictionary - Poorish read speed, no locking required but more allocations require to update than a dictionary.
This sort of guidance usually only comes up when implementation tradeoffs are being made but I’d love to spend more time documenting details like this…
Turns out there's some nice docs on this, see
This post is from an internal conversation I had with @stephentoub. The architect for the .NET libraries. We’ve been having discussions about how to write better recommendations for types like this.
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.
