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
, then MemoryStream
may be more convenient, but if you return a byte[]
you can always just wrap that in a new MemoryStream(blob)
anyway.
It might also depend on how big it is and how long you are holding it for; MemoryStream
can be oversized, which has advantages and disadvantages. Forcing it to a byte[]
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 of MemoryStream
. However, that may be a false advantage: most code won't need to access the byte[]
from multiple threads.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…