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.