acarlon
acarlon

Reputation: 17274

Is there any reason to use CopyToAsync between two MemoryStreams

You know both streams are memory streams. Both streams are backed by byte buffers in memory, so is there any point using CopyToAsync vs CopyTo? A trivial example:

MemoryStream stream1 = new MemoryStream();
MemoryStream stream2 = new MemoryStream();
...//do something to add data to stream1
await stream1.CopyToAsync(stream2);

Upvotes: 7

Views: 5318

Answers (2)

Stephen Cleary
Stephen Cleary

Reputation: 456507

No, all "asynchronous" methods on MemoryStream are actually synchronous. CopyToAsync and friends only exist on MemoryStream because they exist on Stream.

Using CopyToAsync would make sense if one or the other of the streams had true asynchronous operations, but if you know both streams are MemoryStream, then you can just call CopyTo and get the same behavior.

Upvotes: 7

acarlon
acarlon

Reputation: 17274

hmm, should have looked at the implementation for MemoryStream before asking:

// If destination is a MemoryStream, CopyTo synchronously:
memStrDest.Write(_buffer, pos, n);

So, no point.

Will leave question here in case it helps someone else.

Upvotes: 2

Related Questions