Most efficient way to append arrays in C#?

前端 未结 10 2184
挽巷
挽巷 2020-12-02 19:39

I am pulling data out of an old-school ActiveX in the form of arrays of doubles. I don\'t initially know the final number of samples I will actually retrieve.

What i

10条回答
  •  生来不讨喜
    2020-12-02 20:11

    The solution looks like great fun, but it is possible to concatenate arrays in just two statements. When you're handling large byte arrays, I suppose it is inefficient to use a Linked List to contain each byte.

    Here is a code sample for reading bytes from a stream and extending a byte array on the fly:

        byte[] buf = new byte[8192];
        byte[] result = new byte[0];
        int count = 0;
        do
        {
            count = resStream.Read(buf, 0, buf.Length);
            if (count != 0)
            {
                Array.Resize(ref result, result.Length + count);
                Array.Copy(buf, 0, result, result.Length - count, count);
            }
        }
        while (count > 0); // any more data to read?
        resStream.Close();
    

提交回复
热议问题