MemoryStream.TryGetBuffer return 什么时候会有一个有用的ArraySegment?
When will MemoryStream.TryGetBuffer return a useful ArraySegment?
bool MemoryStream.TryGetBuffer(out ArraySegment<byte> buffer)
是 .NET 4.6 中的新 API,可用于访问存储在 MemoryStream 中的有效字节,而无需复制它们。这非常令人兴奋!它 return 是一个布尔值 "true if the conversion was successful; otherwise, false" 并填充输出参数。
什么时候 return 为真,表示 out ArraySegment<byte> buffer
现在包含有效信息?今天没有记录。
我知道如果它 return 为假,我可以使用 .ToArray()
to get a copy of the bytes. And, we've have .GetBuffer()
, but sometimes MemoryStreams are created with an offset into the buffer, and this information is hard (well, sort of) to get later on,更不用说稳健性所需的 try ... catch
。
为了 TryGetBuffer
执行成功的转换并用有用的信息填充输出参数,缓冲区必须可见。如果使用这些构造函数中的任何一个,缓冲区是可见的:
MemoryStream()
MemoryStream(int capacity)
MemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible)
与 publiclyVisible: true
.
查看 source code 了解更多详情。
GetBuffer return内存中的所有字节,独立使用,
例如:
容量 = 100000,长度 = 200
GetBuffer(和 TryGetBuffer !?)return 字节(容量)
ToArray return 字节(长度)
bool MemoryStream.TryGetBuffer(out ArraySegment<byte> buffer)
是 .NET 4.6 中的新 API,可用于访问存储在 MemoryStream 中的有效字节,而无需复制它们。这非常令人兴奋!它 return 是一个布尔值 "true if the conversion was successful; otherwise, false" 并填充输出参数。
什么时候 return 为真,表示 out ArraySegment<byte> buffer
现在包含有效信息?今天没有记录。
我知道如果它 return 为假,我可以使用 .ToArray()
to get a copy of the bytes. And, we've have .GetBuffer()
, but sometimes MemoryStreams are created with an offset into the buffer, and this information is hard (well, sort of) to get later on,更不用说稳健性所需的 try ... catch
。
为了 TryGetBuffer
执行成功的转换并用有用的信息填充输出参数,缓冲区必须可见。如果使用这些构造函数中的任何一个,缓冲区是可见的:
MemoryStream()
MemoryStream(int capacity)
MemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible)
与publiclyVisible: true
.
查看 source code 了解更多详情。
GetBuffer return内存中的所有字节,独立使用,
例如: 容量 = 100000,长度 = 200
GetBuffer(和 TryGetBuffer !?)return 字节(容量)
ToArray return 字节(长度)