Stashbox by Peter Csajtai

<PackageReference Include="Stashbox" Version="5.4.2-preview-760" />

 ImmutableBucket<TValue>

class ImmutableBucket<TValue>
using System; using System.Runtime.CompilerServices; namespace Stashbox.Utils.Data.Immutable { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] internal class ImmutableBucket<[System.Runtime.CompilerServices.Nullable(2)] TValue> { public static readonly ImmutableBucket<TValue> Empty = new ImmutableBucket<TValue>(Constants.EmptyArray<TValue>()); public readonly int Length; public readonly TValue[] Repository; public TValue this[int i] { get { return Repository[i]; } } public ImmutableBucket(TValue value) : this(new TValue[1] { value }) { } public ImmutableBucket(TValue[] repository) { Repository = repository; Length = repository.Length; } internal ImmutableBucket<TValue> Add(TValue value) { if (Length == 0) return new ImmutableBucket<TValue>(new TValue[1] { value }); TValue[] array = new TValue[Length + 1]; Array.Copy(Repository, array, Length); array[Length] = value; return new ImmutableBucket<TValue>(array); } internal ImmutableBucket<TValue> Insert(int index, TValue value) { if (index > Length - 1) throw new IndexOutOfRangeException(); if (Length == 0) return new ImmutableBucket<TValue>(new TValue[1] { value }); TValue[] array = new TValue[Length + 1]; Array.Copy(Repository, array, index); array[index] = value; Array.Copy(Repository, index, array, index + 1, Length - index); return new ImmutableBucket<TValue>(array); } internal ImmutableBucket<TValue> ReplaceAt(int index, TValue value) { if (index > Length - 1) throw new IndexOutOfRangeException(); if (Length == 0) return new ImmutableBucket<TValue>(new TValue[1] { value }); TValue[] array = new TValue[Length]; Array.Copy(Repository, array, Length); array[index] = value; return new ImmutableBucket<TValue>(array); } } }