Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the socially acceptable way to leave a Stream after using it?

Tags:

c#

If you are designing an API, or even within your own code, and your method accepts a Stream, is it your method's duty to check the position and reset it to the beginning provided that CanSeek is true before using it?

If so, why isn't there a method that does all this on the Stream class itself?

Over the years I've been caught out a few times by assuming the Stream comes into my method in position 0.

And is it right to reset the Stream where possible after using it?

Alternatively, should Streams always be copied around instead of passed around directly? Seems a bit excessive to me.

like image 629
Luke Puplett Avatar asked Aug 15 '13 15:08

Luke Puplett


1 Answers

is it your method's duty to check the position and reset it to the beginning provided that CanSeek is true before using it?

No, in that case I would expect the calling code to prepare the stream correctly.

The most common patterns that share a stream over methods require that the pointer is left exactly after the last read/write, so this never comes up much.

But the best thing to after using it: do nothing.

like image 193
Henk Holterman Avatar answered Sep 19 '22 12:09

Henk Holterman