ITADN

[Proposal]: Allow arrays as CollectionBuilder Create parameter type

#9923Openjnm2 创建于 2026-01-07
Proposal champion
J
jnm2commented
# Allow arrays as CollectionBuilder Create parameter type * Specification: https://github.com/dotnet/csharplang/pull/9922 * Discussion: https://github.com/dotnet/csharplang/discussions/8164 ## 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. ```cs [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 <!-- Link to design notes that affect this proposal, and describe in one sentence for each what changes they led to. -->
0 条评论