Stream.ReadByte Stream.ReadByte Stream.ReadByte Stream.ReadByte Method


从流中读取一个字节,并将流内的位置向前提升一个字节,或者如果已到达流结尾,则返回 -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.