Developer knowledge network · moderated exchange

مجتمع الكود غير الموثوق به

أبحاث المطورين، مجتمع الهندسة العكسية والترميز

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 months ago · 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 months ago · 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 months ago · 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.