Developer knowledge network · moderated exchange

Сообщество UnreliableCode

Сообщество разработчиков, обратного проектирования и кодирования

Tutorial

Immutable Collections in C#: ImmutableArray<T> vs ReadOnlyCollection<T> [StackOverflow Architecture Guide]

dotnet_runtime_architect
.NET Core Specialist
MEMBER
Представитель: 103
Дата присоединения: Apr 2018
Сообщения: 40
Спасибо: 24
1 месяцев назад · Jul 18, 2026 12:12 AM
#1

Why ReadOnlyCollection<T> is not truly immutable and how ImmutableArray<T> fixes it:

  • ReadOnlyCollection<T> is just a wrapper around an existing list. If another thread modifies the underlying List<T>, the ReadOnlyCollection changes underneath you (not thread-safe!).
  • ImmutableArray<T> is a readonly struct wrapping a private immutable array. It is 100% thread-safe, has zero wrapper object heap allocation, and allows lock-free reads across all threads without risk of modification.
generic_math_geek
C# 11 Math Guru
MEMBER
Представитель: 138
Дата присоединения: Jul 2021
Сообщения: 10
Спасибо: 88
1 месяцев назад · Jul 18, 2026 3:39 AM
#2

ImmutableArray<T> with Builder (builder.ToImmutable()) is the best choice for shared configuration states.

roslyn_source_gen
Roslyn Compiler Dev
MEMBER
Представитель: 120
Дата присоединения: Feb 2020
Сообщения: 12
Спасибо: 75
1 месяцев назад · Jul 18, 2026 1:37 PM
#3

Because ImmutableArray<T> is a value-type struct, its memory footprint is identical to a raw array pointer.