Search code examples
c#streamingbinaryreader

EndOfStream for BinaryReader


BinaryReader does not have EndOfStream property. Is it safe to use following code to check if end of stream is reached?

reader.BaseStream.Length>reader.BaseStream.Position


Solution

  • It depends. There are various stream types that do not implement the Length or Position property, you'd get a NotSupportedException. NetworkStream for example. Of course, if you'd use such a stream then you really do have to know up front how often to call the BinaryReader.Read() method. So, yes, it's fine.