Allow arrays as CollectionBuilder Create parameter type
Summary
Types may now opt into support for collection expressions by using CollectionBuilderAttribute with a Create method that takes an array rather than a readonly span of the elements.
This will enable types such as ReadOnlyMemory<T> and ArraySegment<T> to support collection expressions without incurring the performance penalty of copying from a span to a second array to use as their backing storage.
[CollectionBuilder(typeof(ReadOnlyMemory), nameof(ReadOnlyMemory.Create))]
public readonly struct ReadOnlyMemory<T> : IEquatable<ReadOnlyMemory<T>> { ... }
// (Or MemoryMarshal, or some other holder)
public static class ReadOnlyMemory
{
// Now allowed: T[] instead of ReadOnlySpan<T> parameter
public static ReadOnlyMemory<T> Create<T>(T[] array) => array;
}
Design meetings
Allow arrays as CollectionBuilder Create parameter type
Summary
Types may now opt into support for collection expressions by using CollectionBuilderAttribute with a Create method that takes an array rather than a readonly span of the elements.
This will enable types such as
ReadOnlyMemory<T>andArraySegment<T>to support collection expressions without incurring the performance penalty of copying from a span to a second array to use as their backing storage.Design meetings