Buff Spread with C#12 collection initializer

Hi, I would like to buff spread with C# 12 collection initializer:

Spread<object> mySpread = ["What", "So", "Ever"];

The changes seems quite lightweight:

using System.Runtime.CompilerServices;

// ...

    [CollectionBuilder(typeof(Spread), nameof(Spread.Create))]
    [Serializable]
    public sealed class Spread<T> : IReadOnlyList<T>, IHasMemory<T>, ISpread, IList<T> /* LINQ looks for IList and not IReadOnlyList */
    {
    // ...
    }

    public static class Spread
    {

        // Collection expression support
        public static Spread<T> Create<T>(ReadOnlySpan<T> items)
        {
            if (items.Length > 0)
                return new Spread<T>(ImmutableArray.Create(items));
            return Spread<T>.Empty;
        }

      // ...
   }

Are there any cons against submitting pull request?
The only side effect this is .net8.0+

image

Test seems to be working

2 Likes

Pretty sure I made a similar change already - let me check.

Ah, it was done in a rather conservative way. Your approach seems to be better. Consider it merged! Thanks!

1 Like