BanksySan
BanksySan

Reputation: 28500

Stream's Length property behaviour when CanSeek = false

I'm implementing the Stream class. The new class supports reading and writing but not seeking.

MSDN says this returns the length of the stream. As it both reading and writing are supported I don't know how long the string will be until all the writing is finished.

Should I throw a NotSupportedException for this as with SetLength and Position?

The documentation doesn't suggest this is the correct behaviour, however, if I do return a value then how should I calculate it?

Upvotes: 4

Views: 684

Answers (1)

DavidG
DavidG

Reputation: 118937

From the documentation for Stream.Length

NotSupportedException: A class derived from Stream does not support seeking.

Additionally from the docs for Stream.CanSeek:

If a class derived from Stream does not support seeking, calls to Length, SetLength, Position, and Seek throw a NotSupportedException.

So I would throw that rather than try to calculate anything, even if you do know the value at the end of the operation.

Upvotes: 1

Related Questions