.NET的MemoryStream - 我应该设置的能力?能力、NET、MemoryStream

2023-09-03 05:46:18 作者:让我以后自己玩。

这可能是一个非常简单的问题,我想的一切,我是后是最佳实践来声明一个新的MemoryStream

This is probably a really simple question, I think all that I am after is Best Practice for declaring a new MemoryStream

什么是以下2个样品之间的差异:

What is the difference between the following 2 samples:

MemoryStream的myStream =新的MemoryStream(0x10000处);

MemoryStream的myStream =新的MemoryStream();

显然,我知道第一实施例设置了流的初始容量。但是,这两种具有自动调整大小的容量。

Obviously, I know that the first example set the initial capacity of the stream. But, both of these have an automatically resizable capacity.

我有什么理由,我应该使用一种方法,而不是其他?

I there any reason why I should use one method as opposed to the other?

推荐答案

还有就是开销大小调整内存流有关。如果您知道或有合理的猜测,需要被存储在内存流预期的大小,你要使用的大小作为初始容量。否则,0则使用缺省大小并作为数据被加到将被调整。

There is overhead associated with re-sizing a memory stream. If you know or otherwise have a reasonable guess as to the expected size needed to be stored in the memory stream, you'll want to use that size as the initial capacity. Otherwise, the default size of 0 is used and will be resized as data is added.