I have a function, which generates and returns a MemoryStream. After generation the size of the MemoryStream is fixed, I dont need to write to it anymore only output is required. Write to MailAttachment or write to database for example.
What is the best way to hand the object around? MemoryStream or Byte Array? If I use MemoryStream I have to reset the position after read.
If you have to hold all the data in memory, then in many ways the choice is arbitrary. If you have existing code that operates on
Stream, thenMemoryStreammay be more convenient, but if you return abyte[]you can always just wrap that in anew MemoryStream(blob)anyway.It might also depend on how big it is and how long you are holding it for;
MemoryStreamcan be oversized, which has advantages and disadvantages. Forcing it to abyte[]may be useful if you are holding the data for a while, since it will trim off any excess; however, if you are only keeping it briefly, it may be counter-productive, since it will force you to duplicate most (at an absolute minimum: half) of the data while you create the new copy.So; it depends a lot on context, usage and intent. In most scenarios, “whichever works, and is clear and simple” may suffice. If the data is particularly large or held for a prolonged period, you may want to deliberately tweak it a bit.
One additional advantage of the
byte[]approach: if needed, multiple threads can access it safely at once (as long as they are reading) – this is not true ofMemoryStream. However, that may be a false advantage: most code won’t need to access thebyte[]from multiple threads.