C#
Stream
File Handling
.NET
Programming

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.

Browse interview questions

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.

csharp
1using System;
2using System.IO;
3using System.Text;
4
5public class Program
6{
7    public static void Main()
8    {
9        using var stream = new MemoryStream();
10        using var writer = new StreamWriter(stream, Encoding.UTF8, leaveOpen: true);
11
12        writer.Write("hello");
13        writer.Flush();
14
15        stream.Position = 0;
16
17        using var reader = new StreamReader(stream, Encoding.UTF8, leaveOpen: true);
18        Console.WriteLine(reader.ReadToEnd());
19    }
20}

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.

csharp
1using System;
2using System.IO;
3using System.Text;
4
5public class Program
6{
7    public static void Main()
8    {
9        using var stream = new MemoryStream(Encoding.UTF8.GetBytes("abcdef"));
10
11        stream.Seek(-2, SeekOrigin.End);
12        Console.WriteLine((char)stream.ReadByte());
13    }
14}

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.

csharp
1if (!stream.CanSeek)
2{
3    throw new NotSupportedException("This stream does not support seeking.");
4}
5
6stream.Position = 0;

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.

csharp
1using var stream = new MemoryStream();
2stream.WriteByte(65);
3stream.WriteByte(66);
4stream.Position = 0;

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 Seek as more correct when the code only needs a simple rewind.
  • Forgetting that buffered wrappers such as StreamWriter may need Flush before repositioning.
  • Calculating offsets manually with Position when Seek would express the movement more clearly.

Summary

  • 'Position = 0 and Seek(0, SeekOrigin.Begin) are equivalent for a seekable stream reset.'
  • Prefer Position = 0 when the intent is simply to rewind.
  • Prefer Seek when the movement is relative to the current position or end.
  • Always respect CanSeek on stream types that may not support repositioning.
  • Flush buffered writers before resetting the stream for reading.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.