Stream.Seek(0, SeekOrigin.Begin) or Position = 0

ConfusedNoob picture ConfusedNoob · Aug 30, 2011 · Viewed 62.8k times · Source

When you need to reset a stream to beginning (e.g. MemoryStream) is it best practice to use

stream.Seek(0, SeekOrigin.Begin);

or

stream.Position = 0;

I've seen both work fine, but wondered if one was more correct than the other?

Answer

gordy picture gordy · Aug 30, 2011

Use Position when setting an absolute position and Seek when setting a relative position. Both are provided for convenience so you can choose one that fits the style and readability of your code. Accessing Position requires the stream be seekable so they're safely interchangeable.