I am having trouble conceptualizing the usages for the new Span in C#.
What construct(s) does it replace? Is ArraySegment now obsolete?
What fu
Span
does not replace anything. It's value added. It provides a type-safe view into continuous segments of memory which can be allocated in many different ways: either as a managed array, a stack-based memory or unmanaged memory.
ArraySegment
is limited to managed arrays. You can't use it to wrap data allocated on the stack using stackalloc
. Span
allows you to do that.
ArraySegment
also does not provide a read-only view into the underlying array. ReadOnlySpan
gives you that.
Span
is not supposed to replace arrays. At the end of the day it's just a view into data. That data has to be allocated somehow, and in managed world that allocation, for most cases, will be an array allocation. So you still need arrays.
You should use Span
if you want your code to be able to manipulate more than just arrays. E.g. consider a parsing library. Right now, to allow it to work with arrays, stack allocated memory and unmanaged memory it has to provide multiple entry points in the API for each of these, and use unsafe code to actually manipulate the data. It also probably would need to expose a string
-based API to be used by people who have their data allocated as strings. With Span
and ReadOnlySpan
you can merge all that logic to a single, Span
-based solution which will be applicable in all these scenarios.
Span
is definitely not going to be something that's used by everybody and very often. It's a highly specialized part of .NET framework useful mostly to library authors and in very high performance critical scenarios. E.g. Kestrel, the web service behind ASP.NET Core will get a lot of performance benefits from moving to Span
because e.g. parsing the request can be done using Span
and stack-allocated memory, which puts no pressure on GC. But you, writing websites and services based on ASP.NET Core don't necessary have to use it.