Span<T> — stack-allocated, ref struct for slicing data without allocation.
1// Slice without allocation2Span<char> span = stackalloc char[256];3"Hello World".AsSpan().CopyTo(span);45// String slicing6ReadOnlySpan<char> hello = "Hello World".AsSpan(0, 5);
Memory<T> — heap-allocatable version of Span<T>.
1Memory<byte> buffer = new byte[1024];2Memory<byte> slice = buffer.Slice(0, 512);3await stream.ReadAsync(buffer);
Pipelines (System.IO.Pipelines):
1var reader = PipeReader.Create(stream);2ReadResult result = await reader.ReadAsync();3ReadSequence<byte> sequence = result.Buffer;
Performance tips: