Stream.ReadByte 方法


从流中读取一个字节,并将流内的位置向前提升一个字节,或者如果已到达流结尾,则返回 -1。Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.

 virtual int ReadByte();
public virtual int ReadByte ();
abstract member ReadByte : unit -> int
override this.ReadByte : unit -> int
Public Overridable Function ReadByte () As Integer


强制转换为 Int32 的无符号字节,如果到达流的末尾,则为 -1。The unsigned byte cast to an Int32, or -1 if at the end of the stream.


流不支持读取。The stream does not support reading.

在流关闭后调用方法。Methods were called after the stream was closed.


CanRead使用属性可确定当前实例是否支持读取。Use the CanRead property to determine whether the current instance supports reading.

在流关闭之后尝试操作流可能会引发ObjectDisposedExceptionAttempts to manipulate the stream after the stream has been closed could throw an ObjectDisposedException.


Stream的默认实现创建新的单字节数组, 然后调用Read(Byte[], Int32, Int32)The default implementation on Stream creates a new single-byte array and then calls Read(Byte[], Int32, Int32). 尽管这是正确的, 但效率低下。While this is formally correct, it is inefficient. 具有内部缓冲区的任何流都应该重写此方法, 并提供一个更高效的版本来直接读取缓冲区, 避免在每次调用时进行额外的数组分配。Any stream with an internal buffer should override this method and provide a much more efficient version that reads the buffer directly, avoiding the extra array allocation on every call.