Choose arrays, lists and read-only collection contracts in C#

An array fixes the number of slots, but callers can still replace the values in those slots. A read-only interface restricts the operations available through that reference, but the underlying collection may still change. Neither fact establishes immutable data.

Choose a collection boundary by deciding who owns its membership, whether consumers should see later changes and whether the elements themselves can change.

Observe the different contracts

Use the .NET 10 SDK. Create a console project in a new directory, then replace Program.cs with the example below.

Plain text
dotnet new console --framework net10.0
dotnet run

Run dotnet run after saving the replacement. No additional packages are required.

C#
using System;
using System.Collections.Generic;
using System.Linq;

var source = new List<int> { 1, 2 };
IReadOnlyList<int> liveView = source;
var snapshot = source.ToArray();
source[0] = 9;
source.Add(3);
Console.WriteLine($"Live view: {liveView[0]}, count {liveView.Count}");
Console.WriteLine($"Array copy: {snapshot[0]}, length {snapshot.Length}");

var wrapper = Array.AsReadOnly(snapshot);
snapshot[0] = 8;
Console.WriteLine($"Wrapper after source edit: {wrapper[0]}");

var protectedCopy = Array.AsReadOnly(source.ToArray());
source[0] = 99;
Console.WriteLine($"Protected copy: {protectedCopy[0]}");
try {
	((IList<int>)protectedCopy)[0] = 100;
} catch (NotSupportedException) {
	Console.WriteLine("Wrapper rejects replacement");
}

var originalItems = new[] { new Item { Name = "bolt" } };
var copiedItems = originalItems.ToArray();
originalItems[0].Name = "nut";
Console.WriteLine($"Shared element: {copiedItems[0].Name}");

/// <summary>Shows why copying a collection does not clone its mutable elements.</summary>
public sealed class Item {
	public string Name { get; set; } = string.Empty;
}

Expected output:

Plain text
Live view: 9, count 3
Array copy: 1, length 2
Wrapper after source edit: 8
Protected copy: 9
Wrapper rejects replacement
Shared element: nut

The read-only reference observes edits to its underlying list. The first array copy has independent slots. Wrapping that array prevents mutation through the wrapper, but an existing reference to the array can still edit it. Copying first and exposing only the wrapper protects membership from those external aliases.

The final case is deliberately different. Both arrays contain a reference to the same mutable Item. Copying the slots did not copy that object. The distinction between a shallow collection copy and independent elements matters for every reference-type collection.

Match the boundary to the promise

Promise Useful choice What it does not promise
Fixed-size materialized data T[] Immutable slots or elements
Growing internal working set List<T> Independent ownership when shared
Indexed read access IReadOnlyList<T> A snapshot or an immutable implementation
Independent membership snapshot A copy such as ToArray() Deep copies of reference-type elements
Prevent caller edits to copied membership Copy, then a read-only wrapper Immutability of mutable element objects

The array documentation distinguishes fixed length from element assignment. Array.AsReadOnly returns a wrapper rather than cloning the array.

For a data-transfer property, a materialized array makes the sequence explicit and avoids returning an unevaluated query. Copy on input or output when independent ownership is part of that property's contract. For an algorithm that merely reads values, an enumerable method parameter can accept a broader range of sources without requiring a list.

Do not treat a type name as an ownership proof

Returning List<T> does not by itself tell the caller whether it is a new list or shared state. Returning IReadOnlyList<T> does not stop the caller from recognizing and casting a concrete mutable implementation. If callers must not modify membership, do not hand them the mutable instance and rely on interface typing alone.

A readonly field prevents assigning a different reference to the field after construction. It does not prevent mutations through that reference. Similarly, a getter-only collection property can still expose a mutable object.

Where true immutability is required, use suitable immutable collection types and ensure element values meet the same requirement. A collection of mutable objects does not become deeply immutable because its container is immutable.

Account for timing and cost

Taking a copy costs time and storage proportional to its size, but it can make ownership clear. Do not copy repeatedly in a hot path without measuring or defining when the snapshot is taken. A live view avoids copying while intentionally allowing the reader to observe changes.

None of these wrappers synchronizes concurrent updates. Coordinate snapshot creation with writers when the source can change concurrently. Document whether callers receive a live view or a snapshot and how long that result remains meaningful.

Source and further reading

This article revisits the problem discussed in the following original answer. The explanation and examples here are newly written for .NET 10.

The array reference type - C# reference
Store multiple variables of the same type in an array data structure in C#. Declare an array by specifying a type or specify Object to store any type.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/arrays
List<T> Class (System.Collections.Generic)
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-10.0
IReadOnlyList<T> Interface (System.Collections.Generic)
Represents a read-only collection of elements that can be accessed by index.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.ireadonlylist-1?view=net-10.0

Author: Herman Schoenfeld