.net 将两个字节数组合并为一个字节数组的最有效方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7482009/
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
Most efficient way to merge two byte arrays into one byte array?
提问by Nano White
I have two byte arrays. I want to merge these two byte arrays into one byte array.
我有两个字节数组。我想将这两个字节数组合并为一个字节数组。
Usually, I just create a new byte array with length = byte array #1 + byte array #2. Then copy byte array #1 and #2 to the new byte array.
通常,我只是创建一个长度 = 字节数组 #1 + 字节数组 #2 的新字节数组。然后将字节数组#1 和#2 复制到新的字节数组中。
Is there a more efficient way to merge two byte arrays using VB.NET and .NET 4?
是否有更有效的方法来使用 VB.NET 和 .NET 4 合并两个字节数组?
回答by Jon
Your existing approach is the most efficient (for what I think is the commonly understood meaning of "efficient") as long as it is implemented properly.
只要正确实施,您现有的方法是最有效的(因为我认为这是“高效”的普遍理解的含义)。
The implementation should look like this:
实现应该是这样的:
var merged = new byte[array1.Length + array2.Length];
array1.CopyTo(merged, 0);
array2.CopyTo(merged, array1.Length);
回答by Stormenet
In our Tcpclient we like to use Buffer.BlockCopy instead of array.copy.
在我们的 Tcpclient 中,我们喜欢使用 Buffer.BlockCopy 而不是 array.copy。
See this question for more info: Array.Copy vs Buffer.BlockCopy
And this one for hard numbers: Best way to combine two or more byte arrays in C#
有关更多信息,请参阅此问题:Array.Copy vs Buffer.BlockCopy
而这对于硬数字:Best way to combine two or more byte array in C#

