I am having trouble conceptualizing the usages for the new Span<T> in C#.
What construct(s) does it replace? Is ArraySegment<T> now obsolete?
What functionality does it enable that previously was not?
Is Span<T> a valid replacement for C# Arrays? In which cases yes, in which cases no?
When will I use an ArraySegment<T> instead of a Span<T>?
I'm trying to understand how my coding styles will need to change to make effective use of the new Span<T>.
How Much Does the Mercedes-Benz C-Class Cost? The 2022 C-Class sedan starts at $43,550. The midtier Exclusive model starts at $45,800, and the top Pinnacle trim has a starting MSRP of $47,500. All-wheel drive is available in all models for an additional $2,000.
In short: Everything. Even though the new C-class's styling isn't a huge departure from the 2021 model, the 2022 model year marks the start of a new generation with more luxury and a greater number of high-tech features.
Is a used Mercedes C-Class saloon reliable? In our most recent reliability survey, the Mercedes C-Class finished at the bottom of the executive car class in 19th place out of a class of 19. A third of C-Classes went wrong, according to owners, who reported a wide range of issues.
Span<T>
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<T>
is limited to managed arrays. You can't use it to wrap data allocated on the stack using stackalloc
. Span<T>
allows you to do that.
ArraySegment<T>
also does not provide a read-only view into the underlying array. ReadOnlySpan<T>
gives you that.
Span<T>
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<T>
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<T>
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<T>
because e.g. parsing the request can be done using Span<T>
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.
From MSDN Magazine: Span is defined in such a way that operations can be as efficient as on arrays: indexing into a span doesn’t require computation to determine the beginning from a pointer and its starting offset, as the ref field itself already encapsulates both. (By contrast, ArraySegment has a separate offset field, making it more expensive both to index into and to pass around.)
Also, while ArraySegment implements IEnumerable
, Span doesn't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With