Skip to content

Is it possible to use NetworkStream.WriteAsync(ReadOnlyMemory<byte>) under .NET Standard 2.0? #47978

Answered by huoyaoyuan
GF-Huang asked this question in Q&A
Discussion options

You must be logged in to vote

Packages can provide additional types, but can't extend members on existing type.

public virtual ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
if (MemoryMarshal.TryGetArray(buffer, out ArraySegment<byte> array))
{
return new ValueTask(WriteAsync(array.Array!, array.Offset, array.Count, cancellationToken));
}
byte[] sharedBuffer = ArrayPool<byte>.Shared.Rent(buffer.Length);
buffer.Span.CopyTo(sharedBuffer);
return new ValueTask(FinishWriteAsync(WriteAsync(sharedBuffer, 0, buffer.Length, c…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@GF-Huang
Comment options

@huoyaoyuan
Comment options

Answer selected by GF-Huang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants