GetBuffer():

Note that the buffer contains allocated bytes which might be unused. 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. To obtain only the data in the buffer, use the ToArray method; however, ToArray creates a copy of the data in memory.

The buffer can also be null.

This method works when the memory stream is closed.

微软官方的解释:请注意,该缓冲区包含这可能是未分配的字节。例如,如果将字符串“测试”被写入MemoryStream对象,该缓冲区的长度从的GetBuffer返回的是256,而不是4,具有252个字节未使用。为了获得只在缓冲区中的数据,使用ToArray方法;然而,ToArray在内存中创建了数据的副本。

ToArray() :

This method omits unused bytes in MemoryStream from the array. To get the entire buffer, use the GetBuffer method.

This method returns a copy of the contents of the MemoryStream as a byte array.

微软官方的解释:此方法返回内存流内容副本作为一个字节数组。(真实数据)

MemoryStream 的GetBuffer() 和 ToArray()的区别的更多相关文章

  1. GetBuffer 与ToArray区别,解决问题场景

    GetBuffer 是把 stream 中的 buffer 的引用传递出来, buffer 的大小是由 stream的 Capacity来决定的. 因为只是地址的引用传递,所以 GetBuffer() ...

  2. MemoryStream类

    转自:http://www.cnblogs.com/kissdodog/archive/2013/01/20/2868864.html MemoryStream 是一个特例,MemoryStream中 ...

  3. c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换

    字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...

  4. C#中MemoryStream类的介绍

    MemoryStream位于System.IO命名空间,为系统内存提供流式的读写操作.常作为其他流数据交换时的中间对象操作. 1.MemoryStream类封装一个字节数组,在构造实例时可以使用一个字 ...

  5. Loading Xps from MemoryStream

    A common way of loading XpsDocument is to load it from file: XpsDocument document = new XpsDocument( ...

  6. MemoryStream转string

    MemoryStream rtfTx = new MemoryStream(); var bs = rtfTx.ToArray(); string s = Encoding.UTF8.GetStrin ...

  7. C# Bitmap类型与Byte[]类型相互转化

    Bitmap   =>   byte[]  Bitmap b = new Bitmap( "test.bmp "); MemoryStream ms = new Memory ...

  8. 2018.8.14-C#复习笔记总

    using System; using System.Collections.Generic; //using System.Linq; using System.Text; using System ...

  9. 容器--Collection和AbstractCollection

    一.前言 容器是JAVA中比较重要的一块,整个体系设计得非常好,同时对于代码学习来说也是比较好的范例.同时很多面试官也比较喜欢用容器来考察面试者的基础知识,所以掌握好容器还是比较重要的.本文主要总结一 ...

随机推荐

  1. Servlet程序访问的流程

    方式一: jsp: <font color="red">${ msg }</font> <form action="/personal/re ...

  2. linux常用方法

    同步时间 ntpdate us.pool.ntp.org 查看http的并发请求数及其TCP连接状态 netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in ...

  3. android学习笔记三

    GUI==>Graphics User Interface,图形用户界面. android UI 建立在View.ViewGroup基础上,采用组合器设计模式设计View和ViewGoup. V ...

  4. SQL常用方法整理

    去除字符串重复项: declare @str varchar(8000) declare @ret varchar(8000),@return varchar(8000) select @str = ...

  5. SVN并行开发管理策略

    总的原则:trunk保证相对稳定.分支合并到主干时将冲突降至最低. (1)       trunk用于集成.测试.发布,可以提交fixbug代码,但不允许直接提交新特性. (2)       特性在分 ...

  6. C#中的 int?是什么意思

    http://www.cnblogs.com/firstcsharp/archive/2011/12/11/2283797.html int?:表示可空类型,就是一种特殊的值类型,它的值可以为null ...

  7. Ubuntu 16.04 LTS (Xenial Xerus)

    Canonical刚刚正式发布了Ubuntu 16.04 LTS (Xenial Xerus),这是一个长期支持版本,官方会提供长达5年的技术支持(包括常规更新/Bug修复/安全升级),一直到2021 ...

  8. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)

    转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...

  9. HDU 2717 Catch That Cow(BFS)

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  10. NeHe OpenGL教程 第十三课:图像字体

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...