When to Use FrozenDictionary in C# for Read-Heavy Lookups
Some dictionaries are built during application startup and then queried thousands of times without changing. For that shape of work, .NET offers FrozenDictionary<TKey,TValue>: an immutable dictionary optimized for frequent reads after a one-time build. It can be a good fit for a stable lookup table, but it is not a faster drop-in replacement for every Dictionary.
This article is for C# developers choosing a collection for configuration-derived or application-owned lookup data. The example targets .NET 8 or later and uses the System.Collections.Frozen namespace. Measure with your real keys and workload before changing a hot path.
Editorial illustration: pay a one-time construction cost for a stable table that serves repeated lookups.
Summary
FrozenDictionarybelongs to the .NET collections API, not a new C# language feature.- It is immutable and designed for dictionaries created infrequently and read frequently.
- Build it once from trusted keys, then share it for lookups; do not rebuild it for each request.
- Keep
Dictionarywhen the data changes often, the table is small, or construction cost outweighs lookup gains.
A practical example
For a small set of stable HTTP status descriptions, build a frozen dictionary once:
using System.Collections.Frozen;
var statusText = new Dictionary<int, string>
{
[200] = "OK",
[404] = "Not Found",
[503] = "Service Unavailable"
};
FrozenDictionary<int, string> descriptions = statusText.ToFrozenDictionary();
if (descriptions.TryGetValue(404, out string? text))
{
Console.WriteLine(text); // Not Found
}
The ToFrozenDictionary() extension creates a frozen copy. The source dictionary can then be discarded or kept if the application still needs a mutable version. After construction, the frozen collection exposes lookup operations but no API for adding or replacing entries.
For a web application, prefer building this at startup or in a static initialization path, not inside a controller action. Otherwise, every request pays the cost that the collection is meant to amortize.
Why freezing can help
Microsoft describes FrozenDictionary as immutable and optimized for situations where a dictionary is created infrequently but used frequently at runtime. Its construction cost is relatively high; its lookup performance can be excellent. The implementation can specialize around the key set and comparer chosen during construction.
That trade-off suggests a useful decision rule: the more reads each stable instance serves, the easier it is to justify the one-time setup cost. A locale table, fixed command map, or application-owned protocol mapping may suit the pattern. A user-editable feature-flag map that reloads every few seconds probably does not.
Frozen collections are immutable, which also makes shared access straightforward: callers cannot accidentally mutate the table through the frozen dictionary API. That does not make the values themselves deeply immutable. If the value is a mutable object, code holding that object may still change its state.
Choose the comparer before freezing
If keys are strings, choose the comparison behavior deliberately. For example, an HTTP header-name map may need case-insensitive matching, while a file-name or domain-specific key may have different rules. Use the overload that takes an IEqualityComparer<TKey> when needed:
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
["Content-Type"] = "application/json",
["Cache-Control"] = "no-store"
};
var frozenHeaders = headers.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);
Avoid assuming that a dictionary’s default string behavior matches your domain. A comparer is part of what defines key equality, so test lookups with case variants or culture-sensitive characters that matter to your application.
When a regular Dictionary is the better tool
Use Dictionary<TKey,TValue> when entries are added or removed during normal operation, when the collection is short-lived, or when you rebuild it frequently. A frozen collection is not intended to absorb ongoing writes. You could build a new frozen snapshot and replace a reference when configuration changes, but that introduces lifecycle and concurrency decisions that should be intentional.
For small tables, simplicity can matter more than a benchmark result. If a lookup is not a measured bottleneck, keeping a normal dictionary may be easier to maintain. Benchmark startup time, memory use, and lookup throughput in the application that will run it; microbenchmarks with different key distributions can point in the wrong direction.
If missing keys are normal, continue using TryGetValue instead of indexing and catching an exception. See this earlier guide to handling missing keys with Dictionary.TryGetValue.
Common mistakes
- Freezing on every call. Construct the frozen table once and reuse it.
- Freezing untrusted, attacker-controlled keys. Microsoft advises initializing frozen dictionaries with trusted keys because key details affect construction time.
- Assuming all values are immutable. The collection’s structure is frozen; referenced objects can still be mutable.
- Choosing a comparer by accident. Match equality semantics to the keys in your domain.
- Claiming a speedup without measurement. Benchmark the actual read/write pattern and include build time if the table is rebuilt.
My Take
FrozenDictionary is a workload-specific tool, not a collection upgrade. I would consider it for a stable table with many lookups, especially if it is created once at startup. If the data changes often or performance is not a measured concern, a regular dictionary is clearer and easier to update.
Conclusion
Use FrozenDictionary when a trusted key set is built rarely and read repeatedly. Choose the comparer before building, share the resulting instance, and measure the whole lifecycle. For frequently changing data, use a mutable collection or deliberately publish new immutable snapshots.