David Fowler πŸ‡§πŸ‡§πŸ‡ΊπŸ‡ΈπŸ’‰πŸ’‰
David Fowler πŸ‡§πŸ‡§πŸ‡ΊπŸ‡ΈπŸ’‰πŸ’‰

@davidfowl

5 Tweets 19 reads Oct 03, 2021
.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…

Loading suggestions...