Span<T> struct
Last updated
Last updated
Provides a type- and memory-safe representation of a contiguous region of arbitrary memory.
Span<T>
is a that is allocated on the stack rather than on the managed heap. Ref struct types have a number of restrictions to ensure that they cannot be promoted to the managed heap:
can't be boxed
they can't be assigned to variables of type
dynamic
or to any interface type
can't be fields in a reference type
can't be used across await
and yield
boundaries
In addition, calls to two methods, and , throw a .
Note: if you need to overcome this restrictions - consider using . It represents a contiguous region of memory as well.,but isn't . So Memory<T> can be placed on the heap.
For spans that represent immutable or read-only structures, use .
A Span<T>
represents a contiguous region of arbitrary memory. A Span<T>
instance is often used to hold the elements of an array or a portion of an array. Unlike an array, however, a Span<T>
instance can point to managed memory, native memory, or memory managed on the stack.
The following example creates a slice of the middle five elements of a 10-element integer array. Note that the code doubles the values of each integer in the slice. As the output shows, the changes made by the span are reflected in the values of the array.C#Copy
Creates a new string to hold the substring.
Copies a subset of the characters from the original string to the new string.
Following example won't create new string objects:
Span<T>
includes two overloads of the method that form a slice out of the current span that starts at a specified index. This makes it possible to treat the data in a Span<T>
as a set of logical chunks that can be processed as needed by portions of a data processing pipeline with minimal performance impact. For example, since modern server protocols are often text-based, manipulation of strings and substrings is particularly important. In the class, the major method for extracting substrings is . For data pipelines that rely on extensive string manipulation, its use offers some performance penalties, since it: