Read-only and immutable collections
Read-only and immutable collections
Read-only access restricts what a caller can do; immutable collections preserve a particular collection version. These are different promises, especially when another reference can change the original list.
Examples target the proposed C# 14 / .NET 10 console baseline. Each is a separate Program.cs; outputs are predictions pending compilation.
Observe a live read-only view
List<T>.AsReadOnly returns a wrapper that disallows edits through the wrapper. It does not copy the list. Changes through the original list remain visible. A read-only interface alone is an even narrower promise: a caller may still have another mutable reference. Use a wrapper for a live view when that is the intended contract.
using System;using System.Collections.Generic;var source = new List<string> { "draft" };var view = source.AsReadOnly();source.Add("review");Console.WriteLine(view.Count);Console.WriteLine(view[1]);The wrapper exposes the same underlying items after the source grows.
Expected output (reasoned, not executed):
2reviewCreate and retain immutable versions
System.Collections.Immutable provides immutable collection types. An operation such as ImmutableList<T>.Add returns a collection with the new contents rather than modifying the original version. Retain the returned value. Implementations may share internal structure, so do not equate every change with a full copy. The example targets the proposed net10.0 console environment; verify the framework reference or approved package availability before execution.
using System;using System.Collections.Immutable;var original = ImmutableList.Create("Monday");var revised = original.Add("Tuesday");Console.WriteLine(original.Count);Console.WriteLine(revised.Count);Console.WriteLine(revised[1]);The new version has two members and the old one retains its original contents.
Expected output (reasoned, not executed):
12TuesdayChoose snapshot depth and lookup behavior
A frozen dictionary in System.Collections.Frozen (introduced in .NET 8) is an immutable mapping optimized for a build-once, read-many pattern. Creating it has a cost; it is not automatically faster for every workload. Neither frozen nor immutable collections automatically freeze mutable objects stored as elements. Concurrent collections solve a different issue: coordinating certain operations across threads; they do not make a sequence of operations automatically atomic.
using System;using System.Collections.Generic;using System.Collections.Frozen;var source = new Dictionary<string, List<int>> { ["A"] = new() { 1 } };var frozen = source.ToFrozenDictionary();source["B"] = new() { 2 };source["A"].Add(3);Console.WriteLine(frozen.ContainsKey("B"));Console.WriteLine(frozen["A"].Count);The new key is absent from the frozen mapping, but both mappings still refer to the same mutable list for A.
Expected output (reasoned, not executed):
False2Common mistakes and edge cases
Ignoring the return from an immutable update discards the new version. A read-only wrapper is not a snapshot. A shallow snapshot retains object references; use immutable element types or deliberate copies where stable element state is required. Do not infer thread-safety for mutable values from the collection’s name.
Practice and next steps
Expose a list of strings as a live read-only view and create an immutable snapshot. Add one string to the original, then add a different string to the immutable list by retaining the returned version. Compare all three counts and explain the intended contract.
Next: Delegates as callable values.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Read-only and immutable collections — knowledge check
Apply the lesson concepts to the following scenarios.
Does AsReadOnly make a copy of the source list?
What must you do with ImmutableList.Add to retain the addition?
A frozen dictionary holds mutable List<int> values. Can a shared list still change?
Which workload suits a frozen dictionary?
What does a concurrent collection primarily address?