Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage / Disadvantage MemoryStream.Position or MemoryStream.Seek [duplicate]

What is the advantage or disadvantage (or the difference) to use

memoryStream.Seek(0, SeekOrigin.Begin);

instead of

memoryStream.Position = 0

like image 729
SeToY Avatar asked Mar 24 '12 12:03

SeToY


People also ask

What is MemoryStream Seek?

MemoryStream Seek Sets the position within the current stream to the specified value.

What is MemoryStream in C#?

MemoryStream(Byte[]) Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array. MemoryStream(Byte[], Boolean) Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array with the CanWrite property set as specified.

What is Seek in stream c#?

Sets the current position of this stream to the given value.


2 Answers

The only advantage of Position is a shorter, more direct notation.

The advantage of Seek(0, SeekOrigin.Begin) is that you also have SeekOrigin.Current and SeekOrigin.End.

But they are functionally the same, pick whatever you think is most readable.

like image 100
Henk Holterman Avatar answered Sep 29 '22 16:09

Henk Holterman


They're both the same internally and set the position of the stream. See MSDN Stream.Seek. Position is absolute while Seek provides a relative / offset position.

Whatever you prefer for readability.

Stream.Position += 50;
Stream.Seek(50, SeekOrigin.Current);
like image 22
Alex Avatar answered Sep 29 '22 17:09

Alex