diff --git a/src/libraries/System.Collections.Immutable/ref/System.Collections.Immutable.cs b/src/libraries/System.Collections.Immutable/ref/System.Collections.Immutable.cs index 48a65aa79e57c..f050f59517d5e 100644 --- a/src/libraries/System.Collections.Immutable/ref/System.Collections.Immutable.cs +++ b/src/libraries/System.Collections.Immutable/ref/System.Collections.Immutable.cs @@ -93,11 +93,11 @@ public static partial class ImmutableArray public static System.Collections.Immutable.ImmutableArray ToImmutableArray(this System.Collections.Generic.IEnumerable items) { throw null; } public static System.Collections.Immutable.ImmutableArray ToImmutableArray(this System.Collections.Immutable.ImmutableArray.Builder builder) { throw null; } } - public partial struct ImmutableArray : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IList, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlyList, System.Collections.ICollection, System.Collections.IEnumerable, System.Collections.IList, System.Collections.Immutable.IImmutableList, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable, System.IEquatable> + public readonly partial struct ImmutableArray : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IList, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlyList, System.Collections.ICollection, System.Collections.IEnumerable, System.Collections.IList, System.Collections.Immutable.IImmutableList, System.Collections.IStructuralComparable, System.Collections.IStructuralEquatable, System.IEquatable> { - private T[] array; - private object _dummy; - private int _dummyPrimitive; + private readonly T[] array; + private readonly object _dummy; + private readonly int _dummyPrimitive; public static readonly System.Collections.Immutable.ImmutableArray Empty; public bool IsDefault { get { throw null; } } public bool IsDefaultOrEmpty { get { throw null; } } diff --git a/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj b/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj index 7657ca4caec15..5197bc1ad3b8f 100644 --- a/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj +++ b/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj @@ -102,6 +102,7 @@ + @@ -114,6 +115,8 @@ '$(TargetFramework)' == 'netstandard2.0' or $(TargetFramework.StartsWith('net4'))"> + + diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs index f1f14b1c2684a..2a9ca8744d339 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Minimal.cs @@ -51,7 +51,7 @@ public partial struct ImmutableArray : IEnumerable, IEquatable [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] - internal T[]? array; + internal readonly T[]? array; /// /// Initializes a new instance of the struct diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs index 60e35e387ec88..248551c8c44bc 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.cs @@ -10,7 +10,7 @@ namespace System.Collections.Immutable { - public partial struct ImmutableArray : IReadOnlyList, IList, IEquatable>, IList, IImmutableArray, IStructuralComparable, IStructuralEquatable, IImmutableList + public readonly partial struct ImmutableArray : IReadOnlyList, IList, IEquatable>, IList, IImmutableArray, IStructuralComparable, IStructuralEquatable, IImmutableList { /// /// Gets or sets the element at the specified index in the read-only list. diff --git a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableInterlocked.cs b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableInterlocked.cs index 5f99c0624b494..fd4a7877b556b 100644 --- a/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableInterlocked.cs +++ b/src/libraries/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableInterlocked.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; using System.Threading; namespace System.Collections.Immutable @@ -121,7 +122,7 @@ public static bool Update(ref ImmutableArray location, Func newImmutableArray = transformer(new ImmutableArray(oldArray)); @@ -131,7 +132,7 @@ public static bool Update(ref ImmutableArray location, Func(ref ImmutableArray location, Func newImmutableArray = transformer(new ImmutableArray(oldArray), transformerArgument); @@ -175,7 +176,7 @@ public static bool Update(ref ImmutableArray location, Func(ref ImmutableArray location, FuncThe prior value at the specified . public static ImmutableArray InterlockedExchange(ref ImmutableArray location, ImmutableArray value) { - return new ImmutableArray(Interlocked.Exchange(ref location.array, value.array)); + return new ImmutableArray(Interlocked.Exchange(ref Unsafe.AsRef(in location.array), value.array)); } /// @@ -209,7 +210,7 @@ public static ImmutableArray InterlockedExchange(ref ImmutableArray loc /// The prior value at the specified . public static ImmutableArray InterlockedCompareExchange(ref ImmutableArray location, ImmutableArray value, ImmutableArray comparand) { - return new ImmutableArray(Interlocked.CompareExchange(ref location.array, value.array, comparand.array)); + return new ImmutableArray(Interlocked.CompareExchange(ref Unsafe.AsRef(in location.array), value.array, comparand.array)); } /// diff --git a/src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs b/src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs index 7431621802b1b..7349cefa67b5c 100644 --- a/src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs +++ b/src/libraries/System.Collections.Immutable/tests/ImmutableArrayTest.cs @@ -16,7 +16,7 @@ namespace System.Collections.Immutable.Tests { public class ImmutableArrayTest : SimpleElementImmutablesTestBase { - private static readonly ImmutableArray s_emptyDefault; + private static readonly ImmutableArray s_emptyDefault = default; // init explicitly to avoid CS0649 private static readonly ImmutableArray s_empty = ImmutableArray.Create(); private static readonly ImmutableArray s_oneElement = ImmutableArray.Create(1); private static readonly ImmutableArray s_manyElements = ImmutableArray.Create(1, 2, 3);