Developer knowledge network · moderated exchange

UnreliableCode қауымдастығы

Әзірлеушілерді зерттеу, кері инженерия және кодтау қауымдастығы

Knowledge indexТірі
4Categories
919Threads
2.8KЖазбалар
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.