问题描述
Array.Copy 和 Buffer.BlockCopy 两者都做同样的事情,但 BlockCopy
是旨在快速字节级原始数组复制,而 Copy
是通用实现.我的问题是 - 在什么情况下应该使用 BlockCopy
?您应该在复制原始类型数组时随时使用它,还是应该只在为性能编码时使用它?使用 Buffer.BlockCopy
而不是 Array.Copy
有什么固有的危险吗?
Array.Copy and Buffer.BlockCopy both do the same thing, but BlockCopy
is aimed at fast byte-level primitive array copying, whereas Copy
is the general-purpose implementation. My question is - under what circumstances should you use BlockCopy
? Should you use it at any time when you are copying primitive type arrays, or should you only use it if you're coding for performance? Is there anything inherently dangerous about using Buffer.BlockCopy
over Array.Copy
?
推荐答案
由于 Buffer.BlockCopy
的参数是基于字节的而不是基于索引的,你更有可能搞砸你的如果使用 Array.Copy
,我只会在代码的性能关键部分使用 Buffer.BlockCopy
.
Since the parameters to Buffer.BlockCopy
are byte-based rather than index-based, you're more likely to screw up your code than if you use Array.Copy
, so I would only use Buffer.BlockCopy
in a performance-critical section of my code.
这篇关于Array.Copy 与 Buffer.BlockCopy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!