[ACCEPTED]-What is the difference between BufferedStream and MemoryStream in terms of application?-c#

Accepted answer
Score: 38

BufferedStream is just a buffer over an existing stream. MemoryStream is 13 a buffer for the whole stream - it isn't chained 12 to another one. You can ask it to write 11 itself to another stream at any time, but 10 that's not the same thing.

One of the principle 9 reasons for buffering is to avoid frequent 8 writes to expensive resources. However, that 7 doesn't mean you want to buffer all the data 6 in memory - just enough to avoid very small 5 writes. For example, if FileStream didn't have its 4 own buffering strategy, then wrapping it in 3 BufferedStream could end up with a buffer of only 8K even 2 if you write megabytes of data. As pointed 1 out in the comments though, FileStream has enough buffering that using BufferedStream in conjunction with it is pointless.

Score: 7

BufferedStream must be initialized by some other existing 8 Stream. A simple close triggers the flush 7 of the buffer to the underlying stream. It's 6 needed when working with a non-memory stream 5 but you need (auto)-buffering.

MemoryStream can exist 4 on its own, but also can be flushed to other 3 streams as you said but 'explicitly'.

If 2 your work is only on memory, it's better 1 to use MemoryStream. Otherwise, BufferedStream.

More Related questions