.Net streams: Returning vs Providing

I would prefer "the second way" (operate on a provided stream) since it has a few distinct advantages:

  • You can have polymorphism (assuming as evidenced by your signature you can do your operations on any type of Stream provided).
  • It's easily abstracted into a Stream extension method now or later.
  • You clearly divide responsibilities. This method should not care on how to construct a stream, only on how to apply a certain operation to it.

Also, if you're returning a new stream (option 1), it would feel a bit strange that you would have to Seek again first in order to be able to read from it (unless you do that in the method itself, which is suboptimal once more since it might not always be required - the stream might not be read from afterwards in all cases). Having to Seek after passing an already existing stream to a method that clearly writes to the stream does not seem so awkward.


I see the benefit of Streams is that you don't need to know what you're streaming to.

In the second example, your code could be writing to memory, it could be writing directly to file, or to some network buffer. From the function's perspective, the actual output destination can be decided by the caller.

For this reason, I would prefer the second option.

The first function is just writing to memory. In my opinion, it would be clearer if it did not return a stream, but the actual memory buffer. The caller can then attach a Memory Stream if he/she wishes.

public byte[] DoStuff(...)
{
    var retStream = new MemoryStream();
    //Write to retStream
    return retStream.ToArray();
}