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. JSP下拉选框,级联选择

    前端: <%@ page contentType="text/html;charset=UTF-8" %> <!DOCTYPE html PUBLIC " ...

  2. android ANR

    下面有两篇关于Android ANR的文章,感觉不错,分享一下! [Android实例] [Sundy系列]网上绝无仅有的Log分析教程及例子!android ANRhttp://blog.csdn. ...

  3. LintCode "Copy Books"

    Classic DP. The initial intuitive O(k*n^2) solution is like this: class Solution { public: /** * @pa ...

  4. Puppet master/agent installation on RHEL7

    ==================================================================================================== ...

  5. contesthunter暑假NOIP模拟赛第一场题解

    contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...

  6. POJ题目分类

    POJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一 ...

  7. 6. redis持久化,认证

    一. RDB 快照持久化 (默认) rdb机制,在满足redis.conf中配置的持久化策略后, 自动持久化数据, 默认存储在dump.rdb文件中 策略 : save 900 1 : 900秒内,一 ...

  8. (C++) 基本面试题(整理)

    1.new.delete.malloc.free关系 new/delete是C++的运算符.new 调用构造函数用于动态申请内存,delete调用对象的析构函数,用于释放内存. malloc与free ...

  9. system函数

    system两层含义: 1.正确退出后.还需要再判断,操作成功或者操作失败. 2.错误退出. #include <stdio.h> #include <stdlib.h> #i ...

  10. MapReduce编程实现学习

    MapReduce主要包括两个阶段:一个是Map,一个是Reduce. 每一步都有key-value对作为输入和输出. Map阶段的key-value对的格式是由输入的格式决定的,如果是默认的Text ...