When is GetBuffer() on MemoryStream ever useful?

后端 未结 7 1235
青春惊慌失措
青春惊慌失措 2020-12-05 23:07

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

7条回答
  •  無奈伤痛
    2020-12-05 23:17

    .NET 4.6 has a new API, bool MemoryStream.TryGetBuffer(out ArraySegment buffer) that is similar in spirit to .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.

提交回复
热议问题