liveBook · Manning

Frozen collection

As explained in C# Concurrency: Asynchronous and multithreaded programming

A frozen collection is a collection whose contents are fixed after construction and optimized for read-only access.

A frozen collection is a read-only collection built for data that will not change. Construction is slower than for regular, concurrent, or immutable collections, but read operations are optimized for maximum speed. section 13.4

Use the appropriate frozen type for the data shape:

  • FrozenDictionary<TKey,TValue> provides a read-only counterpart to Dictionary<TKey,TValue>. section 13.4
  • FrozenSet<T> provides a read-only counterpart to HashSet<T>. section 13.4
  • Use ImmutableArray<T> when a frozen, list-like collection is needed; frozen queues and stacks are not provided. section 13.4
  • Frozen collections cannot be modified and do not provide immutable-style methods for creating modified copies. section 13.4

Build a frozen set

Create ordinary data first, then convert it with the appropriate extension method: var data = new List<int> { 1, 2, 3, 4 }; var set = data.ToFrozenSet();

Choose a frozen collection when data is accessed frequently but almost never modified. Rebuilding one after frequent changes can cost more than its faster lookups save; unlike immutable collections, frozen collections are inherently thread safe. section 13.4.1 section 13.5

How C# Concurrency: Asynchronous and multithreaded programming covers it

How deep it goes
Defines the read-only types, construction methods, and performance trade-off, but does not explain their internal lookup algorithms.
Examples
Demonstrates ToFrozenSet from a List and ToFrozenDictionary from Dictionary and List inputs.
What it assumes
Assumes familiarity with regular collections and distinguishes frozen collections from immutable collections.
What you can do after
Operate/configure FrozenSet<T> and FrozenDictionary<TKey,TValue> from existing collections for read-heavy access.
How it connects
Contrasts frozen collections with immutable collections, concurrent collections, Dictionary, HashSet, and ImmutableArray.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C# Concurrency: Asynchronous and multithreaded programming. The section links point to the parts of the book it draws on.