A short look at swapping `Dictionary<TKey, TValue>` for `FrozenDictionary<TKey, TValue>` for configuration maps, lookup tables and feature flags built once at startup and read constantly. It weighs the one-off `ToFrozenDictionary()` cost against fewer allocations and faster lookups on read-only data.
Key points
- `FrozenDictionary<TKey, TValue>` is one of the read-only collection types added in .NET 8.
- Its bucket and entry layout is tuned for static data, so lookups allocate nothing on the heap.
- The article cites benchmarks where read throughput beats a standard dictionary by up to 40%.
- `ToFrozenDictionary()` is expensive, so call it once at startup or during type construction.
- Mutating means rebuilding the whole hash structure, which rules out frozen collections for changing data.
Real-world use
- Microservice configuration maps loaded at startup and read on every request
- Static lookup tables that live in code and never change
- Feature flag sets queried constantly at runtime
This summary was generated by the agent from the original post on DEV Community: #csharp. The full text and all rights belong to its author and publisher. Go to original