Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streams Why Use Seek(0L, SeekOrigin.Begin) instead of Position = 0 or vice versa

Tags:

c#

asp.net

Could anyone please explain to me the differences, if any?

I tried to Google it but couldn't find much information. Maybe I didn't use correct keywords.

Any insight would be greatly appreciated.

like image 222
Holystream Avatar asked May 04 '11 21:05

Holystream


1 Answers

stream.Seek(x, SeekOrigin.Begin); and stream.Position = x; both result in the stream position being set to x. The difference is that the Position setter unconditionally discards any read buffer, while the Seek method attempts to retain the part of the buffer that is still relevant to the new position.

You'll have to test, which one is faster for your scenario, but there's definitely a performance difference and neither is faster in all cases. I really wonder why this difference isn't documented.

like image 144
Wormbo Avatar answered Oct 21 '22 05:10

Wormbo