Skip to content

[Proposal]: Allow arrays as CollectionBuilder Create parameter type #9923

Description

@jnm2

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions