MemoryStream.GetBuffer 方法

定義

傳回用於建立這個資料流之不帶正負號位元組的陣列。Returns the array of unsigned bytes from which this stream was created.

public:
 virtual cli::array <System::Byte> ^ GetBuffer();
public virtual byte[] GetBuffer ();
abstract member GetBuffer : unit -> byte[]
override this.GetBuffer : unit -> byte[]
Public Overridable Function GetBuffer () As Byte()

傳回

Byte[]

位元組陣列 (建立這個資料流的來源) 或基礎陣列 (如果沒有在目前執行個體的建構期間將位元組陣列提供給 MemoryStream 建構函式)。The byte array from which this stream was created, or the underlying array if a byte array was not provided to the MemoryStream constructor during construction of the current instance.

例外狀況

MemoryStream 執行個體沒有使用公開可見的緩衝區來建立。The MemoryStream instance was not created with a publicly visible buffer.

備註

請注意,緩衝區包含可能未使用的已配置位元組。Note that the buffer contains allocated bytes which might be unused. 例如,如果字串 "test" 寫入 MemoryStream 物件中,從 GetBuffer 傳回的緩衝區長度為256,而不是4,未使用252個位元組。For example, if the string "test" is written into the MemoryStream object, the length of the buffer returned from GetBuffer is 256, not 4, with 252 bytes unused. 若只要取得緩衝區中的資料,請使用 ToArray 方法;不過,ToArray 會在記憶體中建立資料的複本。To obtain only the data in the buffer, use the ToArray method; however, ToArray creates a copy of the data in memory.

緩衝區也可以 nullThe buffer can also be null.

若要建立具有公開可見緩衝區的 MemoryStream 實例,請使用 MemoryStreamMemoryStream(Byte[], Int32, Int32, Boolean, Boolean)MemoryStream(Int32)To create a MemoryStream instance with a publicly visible buffer, use MemoryStream, MemoryStream(Byte[], Int32, Int32, Boolean, Boolean), or MemoryStream(Int32). 如果目前的資料流程可以調整大小,則如果在呼叫之間調整基礎位元組陣列的大小,這個方法的兩個呼叫不會傳回相同的陣列。If the current stream is resizable, two calls to this method do not return the same array if the underlying byte array is resized between calls. 如需詳細資訊,請參閱 CapacityFor additional information, see Capacity.

注意

這個方法會在記憶體資料流程關閉時運作。This method works when the memory stream is closed.

適用於

另請參閱