C# 7.2 introduces two new types: Span<T>
and Memory<T>
that have better performance over earlier C# types like string[]
.
Question: What is the difference between Span<T>
and Memory<T>
? Why would I use one over the other?
C# 7.2 introduces two new types: Span<T>
and Memory<T>
that have better performance over earlier C# types like string[]
.
Question: What is the difference between Span<T>
and Memory<T>
? Why would I use one over the other?
re: this means it can only point to memory allocated on the stack.
Span<T>
can point to any memory: allocated either on the stack or the heap. Stack-only nature ofSpan<T>
means that theSpan<T>
itself (not the memory it points to) must reside only on the stack. This is in contrast to "normal" C# structs, that can reside on the stack or on the heap (either via value-type boxing, or when they are embedded in classes/reference-types). Some of the more obvious practical implications are that you cannot have aSpan<T>
field in a class, you cannot boxSpan<T>
, and you cannot make an array of them.Span<T>
is stack-only in nature whileMemory<T>
can exist on the heap.Reference Document: here