Codectionary / Developer documentation / C#

Build Read-Many Lookups with FrozenDictionary

Create a fixed map optimised for lookup-heavy use while keeping construction cost and shallow values in mind.

Read the syntax

Create a fixed map optimised for lookup-heavy use while keeping construction cost and shallow values in mind.

Replacing an entry in the source does not replace the corresponding frozen entry.

What it is and why to use it

A fixed routing table or known-code lookup can be built once and read many times. FrozenDictionary, introduced with .NET 8, is designed for such lookup-heavy workloads. Construction can be more expensive than an ordinary dictionary, so it is not an automatic performance improvement for every small or frequently rebuilt map.

How it works

Use ToFrozenDictionary with a deliberate key comparer. The resulting map exposes no normal entry-mutation API and is not a live view of a mutable source dictionary. This freezes the mapping, not every referenced value object. Enumeration order is not a sorting contract; query known keys or sort explicitly when presentation needs order.

Common mistakes and limitations

Do not rebuild a frozen map on every request without measuring the tradeoff. Do not assume mutable values become deeply immutable. Validate untrusted construction data and duplicate keys before using a map as long-lived configuration.

Run the examples locally

These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.

Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.

C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.

Create and enter a project

dotnet new console -n CSharpExample --framework net10.0
cd CSharpExample

CSharpExample.csproj settings used for these examples

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>12.0</LangVersion>
    <Nullable>enable</Nullable>
    <ImplicitUsings>disable</ImplicitUsings>
  </PropertyGroup>
</Project>

Build and run after saving one example in Program.cs

dotnet build
dotnet run --no-build

Continue learning

Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.

Syntax

var frozen = source.ToFrozenDictionary(StringComparer.Ordinal);

Examples

Detach a fixed mapping from source updates

Create a frozen map and then replace a source value.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Frozen;

var source = new Dictionary<string, string> { ["php"] = "PHP" };
var frozen = source.ToFrozenDictionary(StringComparer.Ordinal);
source["php"] = "Changed";
Console.WriteLine(frozen["php"]);
Console.WriteLine(source["php"]);
  1. ToFrozenDictionary builds the fixed mapping from the source entries.
  2. Replacing an entry in the source does not replace the corresponding frozen entry.
  3. Strings are immutable here. Mutable value objects would require their own ownership policy.
  4. Expected output: PHP Changed
  5. Use fixed lookup structures for genuinely read-many data.

Use a comparer-aware fixed lookup

Handle a known key and a missing key without relying on enumeration order.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Frozen;

var frozen = new Dictionary<string, int> { ["PHP"] = 1, ["C#"] = 2 }
    .ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);
Console.WriteLine(frozen["php"]);
Console.WriteLine(frozen.TryGetValue("Ruby", out _) ? "Found" : "Missing");
  1. The chosen comparer makes php equivalent to PHP.
  2. TryGetValue reports the absence of Ruby without an indexer exception.
  3. The program queries keys directly rather than assuming an ordering of frozen entries.
  4. Expected output: 1 Missing
  5. Use fixed lookup structures for genuinely read-many data.

Best practices

  • Use fixed lookup structures for genuinely read-many data.
  • Treat construction cost and referenced value mutability separately.

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading