我在c#中有3个字节数组,我需要合并成一个。完成这项任务最有效的方法是什么?


当前回答

memorystream类为我很好地完成了这项工作。我无法让缓冲类运行得像内存流一样快。

using (MemoryStream ms = new MemoryStream())
{
  ms.Write(BitConverter.GetBytes(22),0,4);
  ms.Write(BitConverter.GetBytes(44),0,4);
  ms.ToArray();
}

其他回答

    public static bool MyConcat<T>(ref T[] base_arr, ref T[] add_arr)
    {
        try
        {
            int base_size = base_arr.Length;
            int size_T = System.Runtime.InteropServices.Marshal.SizeOf(base_arr[0]);
            Array.Resize(ref base_arr, base_size + add_arr.Length);
            Buffer.BlockCopy(add_arr, 0, base_arr, base_size * size_T, add_arr.Length * size_T);
        }
        catch (IndexOutOfRangeException ioor)
        {
            MessageBox.Show(ioor.Message);
            return false;
        }
        return true;
    }

在我看来,许多答案都忽略了上述要求:

结果应该是一个字节数组 它应该尽可能地高效

这两者一起排除了字节的LINQ序列——任何带有yield的东西都不可能在不遍历整个序列的情况下获得最终大小。

当然,如果这些不是真正的需求,LINQ可能是一个完美的解决方案(或IList<T>实现)。然而,我假设超级哑铃知道他想要什么。

(编辑:我刚刚有了另一个想法。复制数组和惰性读取数组之间有很大的语义差异。考虑一下,如果在调用Combine(或其他方法)方法之后,但在使用结果之前更改其中一个“源”数组中的数据,会发生什么情况——使用惰性求值,该更改将是可见的。如果是即时拷贝,就不会了。不同的情况会要求不同的行为——这是需要注意的。)

以下是我建议的方法——当然,这些方法与其他一些答案中包含的方法非常相似:)

public static byte[] Combine(byte[] first, byte[] second)
{
    byte[] ret = new byte[first.Length + second.Length];
    Buffer.BlockCopy(first, 0, ret, 0, first.Length);
    Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
    return ret;
}

public static byte[] Combine(byte[] first, byte[] second, byte[] third)
{
    byte[] ret = new byte[first.Length + second.Length + third.Length];
    Buffer.BlockCopy(first, 0, ret, 0, first.Length);
    Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
    Buffer.BlockCopy(third, 0, ret, first.Length + second.Length,
                     third.Length);
    return ret;
}

public static byte[] Combine(params byte[][] arrays)
{
    byte[] ret = new byte[arrays.Sum(x => x.Length)];
    int offset = 0;
    foreach (byte[] data in arrays)
    {
        Buffer.BlockCopy(data, 0, ret, offset, data.Length);
        offset += data.Length;
    }
    return ret;
}

当然,“params”版本需要首先创建一个字节数组数组,这会带来额外的效率低下。

可以使用泛型组合数组。下面的代码可以很容易地扩展为三个数组。这样就不需要为不同类型的数组复制代码。对我来说,上面的一些答案似乎过于复杂。

private static T[] CombineTwoArrays<T>(T[] a1, T[] a2)
    {
        T[] arrayCombined = new T[a1.Length + a2.Length];
        Array.Copy(a1, 0, arrayCombined, 0, a1.Length);
        Array.Copy(a2, 0, arrayCombined, a1.Length, a2.Length);
        return arrayCombined;
    }

我以Matt的LINQ为例,进一步提高了代码的洁净度:

byte[] rv = a1.Concat(a2).Concat(a3).ToArray();

在我的例子中,数组很小,所以我不关心性能。

实际上,我在使用Concat时遇到了一些问题…(对于1000万个数组,它实际上崩溃了)。

我发现以下是简单,容易,工作得很好,没有崩溃对我来说,它适用于任何数量的数组(不仅仅是三个)(它使用LINQ):

public static byte[] ConcatByteArrays(params byte[][]  arrays)
{
    return arrays.SelectMany(x => x).ToArray();
}