I have been reading about span for a while now, and just tried to implement it. However, while I can get span to work I cannot figure out how to get a stream to accept it like they do in the examples. Other examples show int.parse supporting spans as well but I can't find overloads or extensions that make it possible.
I have tried it in both .net Standard 2.0 and .net core 2.0.
Please point me in the right direction to make this work.
Edit: Code example would be:
Span<Byte> buffer = new Span<byte>();
int bytesRead = stream.Read(buffer);
Span results from streams are supported in .NET Core 2.1. If you check the current source code of eg Stream you'll see it has overloads like Read(Span) that read into a
Span<byte>
instead ofbyte[]
, or Write(ReadOnlySpan) that can write out aReadOnlySpan<byte>
instead of abyte[]
, overloads that use Memory etc.To target .NET Core 2.1, you'll have to install at least Visual Studio 2017 15.7 Preview 4 or the latest SDK for .NET Core 2.1
Let's look at an example that I have handy, where the
Span<T>
happens to come fromPipeWriter
.Try to get a
Memory<T>
instead ofSpan<T>
, for which you can get the underlying array (except under some exotic circumstances).GetUnderlyingArray()
here is a small extension method:All in all, this lets you use the methods with a 'modern' return type in combination with the 'old' framework overloads - without any copying. :)