No you cannot use Span<T>
in lieu of List<T>
. List<T>
stores a set of items and can grow and shrink over time. Span<T>
is a view into an existing set of data that is optimized to not require memory allocations. If you don't understand what Span<T>
is then you don't need to be using it at this point. There is a full discussion of all this along with plenty of examples in the docs.
The primary purpose of Span<T>
is to eliminate all the little string allocations that are commonly done when working with strings. For example if you need to get the first 4 characters of a string then the standard string functions allocate a new string to store that copy. The goal of Span
is to eliminate the allocation. Many of the string functions have been updated to work with Span
as well. Instead of allocating a new string the span simply provides a view into the existing string limited by the span size you wanted (e.g. 4). It is all about performance and memory management. Again, unless you understand all this then don't worry about Span
right now. It is not going to impact your daily code. Refer to the linked document for more rationales and examples.
The other types you mentioned are just generalizations and specializations over the same concept, a view into an existing set of data without allocating a new object.