MemoryStream.GetBuffer 方法


返回从中创建此流的无符号字节的数组。Returns the array of unsigned bytes from which this stream was created.

 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()



创建此流所用的字节数组;或者如果在当前实例的构造期间没有向 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.

还可以 null缓冲区。The 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.