Span<T>.CopyTo(Span<T>, int index) #73108
-
It seems there's no easy way of copying to a span at a certain index, which would be useful for concatenation (e.g. of bytes). Instead, you're forced to do something like this: public static Span<byte> Concat(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)
{
var array = new byte[a.Length + b.Length];
a.CopyTo(array);
b.CopyTo(array.AsSpan(a.Length));
return array.AsSpan();
} Instead of something like this: Span<byte> buffer = stackalloc byte[32];
a.CopyTo(buffer, index: 0);
b.CopyTo(buffer, index: a.Length);
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 13 replies
-
@samuel-lucas6
|
Beta Was this translation helpful? Give feedback.
-
You can just do |
Beta Was this translation helpful? Give feedback.
You can just do
span1.CopyTo(span2.Slice(index))
orspan1.CopyTo[span2[index..]]
. No more overload is needed.