I have a List<T>
containing some data. I would like to pass it to a function which accepts ReadOnlySpan<T>
.
List<T> items = GetListOfItems();
// ...
void Consume<T>(ReadOnlySpan<T> buffer)
// ...
Consume(items??);
In this particular instance T is byte
but it doesn't really matter.
I know I can use .ToArray()
on the List, and the construct a span, e.g.
Consume(new ReadOnlySpan<T>(items.ToArray()));
However this creates a (seemingly) unneccessary copy of the items. Is there any way to get a Span directly from a List? List<T>
is implemented in terms of T[]
behind the scenes, so in theory it's possible, but not as far as I can see in practice?