I\'ve known that GetBuffer()
on a MemoryStream in C#/.NET has to be used with care, because, as the docs describe here, there can be unused bytes at the end, so
.NET 4.6 has a new API, bool MemoryStream.TryGetBuffer(out ArraySegment.GetBuffer()
. This method will return an ArraySegment that includes the _origin
information if it can.
See this question for details about when .TryGetBuffer()
will return true and populate the out param with useful information.