ITADN

[Proposal question]: Should collection expressions use implicit conversions to ReadOnlySpan for Create methods with a single spread element?

#10178OpenRikkiGibson 创建于 2026-05-14
R
RikkiGibsoncommented
https://github.com/dotnet/roslyn/issues/73110 ```cs using System; using System.Collections.Generic; using System.Runtime.CompilerServices; [CollectionBuilder(typeof(MyCustomCollectionFactory), nameof(MyCustomCollectionFactory.Create))] class MyCustomCollection<T> { public IEnumerator<T> GetEnumerator() => default!; } class MyCustomCollectionFactory { public static MyCustomCollection<T> Create<T>(ReadOnlySpan<T> elements) => default!; } class Program { // Question: Which of these methods, should essentially be implemented as 'Create((ReadOnlySpan<int>)elements)', // and which need to copy the 'elements' to something first (such as a new array)? // Today, the first 2 are implemented as 'Create(elements.ToArray())', and the 3rd is an error. public MyCustomCollection<int> Convert(Span<int> elements) => [.. elements]; public MyCustomCollection<int> Convert(int[] elements) => [.. elements]; public MyCustomCollection<int> Convert(MyArraySlice<int> elements) => [.. elements]; // error today } ref struct MyArraySlice<T> { public static implicit operator ReadOnlySpan<T>(MyArraySlice<T> value) => default!; } ```
0 条评论