.Net streams: return and provision

I always wondered what is the best way to use a class Streamin C # .Net. Is it better to provide a thread that was written or provided alone? i.e:

public Stream DoStuff(...)
{
    var retStream = new MemoryStream();
    //Write to retStream
    return retStream;
}

Unlike;

public void DoStuff(Stream myStream, ...)
{
    //write to myStream directly
}

I have always used the previous example to manage the life cycle, but I have the feeling that this is a poor way to "stream" with Streamthe lack of a better word.

+4
source share
2 answers

I would prefer the "second method" (work with the provided stream), since it has several special advantages:

  • ( , Stream).
  • Stream.
  • . , , , .

, ( 1), , Seek, ( , , - ). Seek , , .

+6

Streams , , .

, - . .

.

- . , , , . , .

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

All Articles