c# MemoryStream 与字节数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11828599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:24:45  来源:igfitidea点击:

c# MemoryStream vs Byte Array

c#asp.net.net

提问by LuckyStrike

I have a function, which generates and returns a MemoryStream. After generation the size of the MemoryStream is fixed, I dont need to write to it anymore only output is required. Write to MailAttachment or write to database for example.

我有一个函数,它生成并返回一个 MemoryStream。生成后 MemoryStream 的大小是固定的,我不再需要写入它只需要输出。例如写入 MailAttachment 或写入数据库。

What is the best way to hand the object around? MemoryStream or Byte Array? If I use MemoryStream I have to reset the position after read.

传递物体的最佳方式是什么?内存流还是字节数组?如果我使用 MemoryStream,我必须在读取后重置位置。

采纳答案by Marc Gravell

If you have to hold all the data in memory, then in many ways the choice is arbitrary. If you have existing code that operates on Stream, then MemoryStreammay be more convenient, but if you return a byte[]you can always just wrap that in a new MemoryStream(blob)anyway.

如果您必须将所有数据保存在内存中,那么在许多方面选择是任意的。如果您有现有的代码,其操作上Stream,则MemoryStream可能会更方便,但如果你返回byte[]你可以随时只是包装在一个new MemoryStream(blob)反正

It might also depend on how big it is and how long you are holding it for; MemoryStreamcan be oversized, which has advantages and disadvantages. Forcing it to a byte[]may be useful if you are holding the data for a while, since it will trim off any excess; however, if you are only keeping it briefly, it may be counter-productive, since it will force you to duplicatemost (at an absolute minimum: half) of the data while you create the new copy.

这也可能取决于它有多大以及你持有它多长时间;MemoryStream可以是超大的,这有优点也有缺点。byte[]如果您将数据保留一段时间,将其强制为 a可能会很有用,因为它会修剪掉任何多余的数据;但是,如果您只是简单地保留它,它可能会适得其反,因为它会迫使您在创建新副本时复制大部分(绝对最少:一半)数据。

So; it depends a lot on context, usage and intent. In most scenarios, "whichever works, and is clear and simple" may suffice. If the data is particularly large or held for a prolonged period, you may want to deliberately tweak it a bit.

所以; 这在很大程度上取决于上下文、用法和意图。在大多数情况下,“无论哪个有效,并且清晰简单”就足够了。如果数据特别大或保存时间过长,您可能需要特意稍微调整一下。

One additional advantage of the byte[]approach: if needed, multiple threads can access it safely at once (as long as they are reading) - this is not true of MemoryStream. However, that may be a false advantage: most code won't needto access the byte[]from multiple threads.

byte[]方法的另一个优点是:如果需要,多个线程可以一次安全地访问它(只要它们正在阅读)——这对MemoryStream. 然而,这可能是一个错误的优势:大多数代码不需要byte[]从多个线程访问。

回答by Mike Perrenoud

Use a byte[] because it's a fixed sized object making it easier for memory allocation and cleanup and holds relatively no overhead - especially since you don't need to use the functions of the MemoryStream. Further you want to get that stream disposed of ASAP so it can release the possible unmanaged resources it may be using.

使用 byte[] 是因为它是一个固定大小的对象,可以更轻松地进行内存分配和清理,并且相对没有开销 - 特别是因为您不需要使用 MemoryStream 的功能。此外,您希望尽快处理该流,以便它可以释放可能正在使用的非托管资源。

回答by user1587368

The MemoryStream class is used to add elements to a stream. There is a file pointer; It simulates random access, it depends on how it is implemented. Therefore, a MemoryStream is not designed to access any item at any time.

MemoryStream 类用于向流中添加元素。有一个文件指针;它模拟随机访问,这取决于它是如何实现的。因此,MemoryStream 并非旨在随时访问任何项目。

The byte array allows random access of any element at any time until it is unassigned.

字节数组允许在任何时间随机访问任何元素,直到它被取消分配。

Next to the byte [], MemoryStream lives in memory (depending on the name of the class). Then the maximum allocation size is 4 GB.

在字节 [] 旁边,MemoryStream 存在于内存中(取决于类的名称)。那么最大分配大小为 4 GB。

Finally, use a byte [] if you need to access the data at any index number. Otherwise, MemoryStream is designed to work with something else that requires a stream as input while you just have a string.

最后,如果您需要访问任何索引号处的数据,请使用字节 []。否则,MemoryStream 旨在处理需要流作为输入的其他内容,而您只有一个字符串。