I have a List
containing some data. I would like to pass it to a function which accepts ReadOnlySpan
.
List i
In .Net 5.0, you can use CollectionsMarshal.AsSpan() (source, GitHub isue) to get the underlying array of a List
as a Span
.
Keep in mind that this is still unsafe: if the List
reallocates the array, the Span
previously returned by CollectionsMarshal.AsSpan
won't reflect any further changes to the List
. (Which is why the method is hidden in the System.Runtime.InteropServices.CollectionsMarshal
class.)