Separated out of https://github.com/dotnet/corefx/issues/21281 for tracking purposes.
```C#
namespace System.IO
{
public class Stream
{
public virtual int Read(Span
public virtual ValueTask
public virtual void Write(ReadOnlySpan<byte> source);
public virtual Task WriteAsync(ReadOnlyBuffer<byte> source, CancellationToken cancellationToken = default(CancellationToken));
…
}
}
```
:bulb: Can start writing = default for the default value, assuming you don't want to go the actual right way and make it required. 😉
++ for the actual right way 😄
@stephentoub there's more than a slim chance that I'm missing something but I don't understand the purpose of the new Span-based Read/Write methods on Stream since the default implementations seem to introduce overhead above that of the array based overloads.
When would/should I choose to call the Span-based versions?
since the default implementations seem to introduce overhead above that of the array based overloads
Those default implementations only exist for cases where derived Streams don't override them. Every important stream in .NET Core does override them, and once this ships, we hope/expect other streams will as well.
Thanks @stephentoub, I did wonder if it was just so you could have extra flexibility for Stream derivatives but I still couldn't understand where you would get a benefit from this flexibility (in the Stream.Read/Write case, I can see benefit in other areas).
What would be a good use case for choosing MemoryStream.Read(Span..) over the overload that takes an array?
What would be a good use case for choosing MemoryStream.Read(Span..) over the overload that takes an array?
a) You have a span as input.
b) You want to avoid the Task<int> allocation.
@tdinucci The use of byte[] as a stand-in for "a buffer of bytes" was just a very long-standing broken API to begin with. It looked familiar to C developers, but was very hard to use in safe code. With spans and buffers there's no reason to even use the array-based API, and you can do every manipulation imaginable in a safe fashion. I wish the array-based APIs were deprecated already... They are useless, since arrays implicitly convert to Span, if you insist on using arrays to begin with (Memory expresses intent better).