Advertisement
Advanced C#, Collections

When would you reach for a ConcurrentDictionary or an immutable collection?

Short answer

Use ConcurrentDictionary for thread-safe reads/writes without external locks; use ImmutableList/Dictionary when you want safe sharing across threads via copy-on-write snapshots.

Save C# mock interview
Advertisement
AModel answer
Use ConcurrentDictionary for thread-safe reads/writes without external locks; use ImmutableList/Dictionary when you want safe sharing across threads via copy-on-write snapshots.
var cache = new ConcurrentDictionary<int,User>();
cache.GetOrAdd(id, Load);
Real-world example
A shared in-memory cache updated by many request threads uses ConcurrentDictionary.