Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Collections.Frozen.FrozenDictionary: practical reference

System.Collections.Frozen.FrozenDictionary: practical reference: FrozenDictionary<TKey,TValue> is an immutable lookup index prepared once for fast and frequent key reads.

Version: .NET 8+; verified on .NET 10.0.14

Syntax

System.Collections.Frozen.FrozenDictionary /* select a documented member or constructor */

FrozenDictionary<TKey,TValue> is an immutable lookup index prepared once for fast and frequent key reads.

Build it for long-lived configuration or lookup data that is read far more often than it changes.

Construction has a cost and fixes the comparer, so it is unsuitable for data that must be rebuilt frequently.

Duplicate keys fail during construction, and later changes to the source collection do not change the frozen dictionary.

The complete program actually uses System.Collections.Frozen.FrozenDictionary; its exact expected stdout is 1.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var map = new Dictionary<string, int> { ["a"] = 1 }.ToFrozenDictionary();
Console.WriteLine(map["a"]);

Result

1

The complete program actually uses `System.Collections.Frozen.FrozenDictionary`; its exact expected stdout is `1`.

Sources