Stream.Seek0, SeekOrigin.Begin or Position 0
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
For a seekable .NET stream, stream.Seek(0, SeekOrigin.Begin) and stream.Position = 0 both move the cursor to the beginning. The real choice is usually about readability and intent: Position = 0 is clearer for resetting to the start, while Seek is more general when you need relative movement from the current position or the end.
Position = 0 is the clearest reset-to-start form
If the goal is simply "go back to the beginning," assigning Position is usually the most readable option.
This says exactly what the code means with no extra parameters to interpret.
Use Seek when the offset matters
Seek is more flexible because it can move relative to the beginning, current position, or end.
That kind of movement cannot be expressed as clearly with Position unless you calculate the new offset yourself.
They are equivalent only for seekable streams
Not every stream supports seeking. FileStream and MemoryStream usually do. NetworkStream usually does not. Before resetting or seeking, check CanSeek if the stream type is not guaranteed.
If you skip this on a non-seekable stream, you will get an exception regardless of whether you used Seek or Position.
Position often reads better in rewinding scenarios
A common use case is writing to a memory stream and then reading it back. In that pattern, Position = 0 is easier to scan.
When a reader sees this, the intent is obvious: the code rewinds before reading. Seek(0, SeekOrigin.Begin) is still correct, but slightly more ceremonial.
Use whichever expresses intent better in the local code
There is no meaningful correctness advantage between the two for the specific case of moving to the start of a seekable stream. Some developers prefer Seek because it mirrors lower-level file APIs. Others prefer Position because it looks like state assignment. The best choice is the one that makes the current operation most obvious.
In code review, the more valuable question is usually whether the stream is actually seekable and whether buffered readers or writers were flushed before repositioning.
Common Pitfalls
- Using either approach on a stream that does not support seeking.
- Rewinding a stream before flushing a writer and then wondering why data is missing.
- Treating
Seekas more correct when the code only needs a simple rewind. - Forgetting that buffered wrappers such as
StreamWritermay needFlushbefore repositioning. - Calculating offsets manually with
PositionwhenSeekwould express the movement more clearly.
Summary
- '
Position = 0andSeek(0, SeekOrigin.Begin)are equivalent for a seekable stream reset.' - Prefer
Position = 0when the intent is simply to rewind. - Prefer
Seekwhen the movement is relative to the current position or end. - Always respect
CanSeekon stream types that may not support repositioning. - Flush buffered writers before resetting the stream for reading.
Related reading
- String interning in .NET Framework - What are the benefits and when to use interning
- String vs string in C
- String was not recognized as a valid DateTime format dd/MM/yyyy
- String.Format - how it works and how to implement custom formatstrings
- string.IsNullOrEmptystring vs. string.IsNullOrWhiteSpacestring
- String.Join method that ignores empty strings?
- String.Join vs. StringBuilder which is faster?
- String.Replace vs. StringBuilder.Replace

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.