Fast way to copy an array into a List?

C#'s List<> has a set of CopyTo functions that will extract the contents of its internal array into another array using a fast memory block copy.

Is there a way to do this in reverse? It might look like...

var buffer = new List<byte>();
buffer.AddRange(afewbytes);
buffer.AddFromArray(myArray, startIndex, countBytesToCopy);
buffer.AddRange(afewmorebytes);

As my List is the List<byte> variety, I'd prefer to avoid a loop that copies byte by byte.

Jon Skeet
people
quotationmark

The List<T>(IEnumerable<T>) constructor will use ICollection<T>.CopyTo if the collection implements ICollection<T>, which byte[] will do.

That's not going to help directly if you only want to extract part of the array, but you could create your own ByteArraySegment class implementing ICollection<byte> and implement the CopyTo operation using Buffer.BlockCopy or whatever:

public class ByteArraySegment : ICollection<byte>
{ 
    private readonly byte[] array;
    private readonly int start;
    private readonly int count;

    public ByteArraySegment(...)
    {
        // Obvious code
    }

    public void CopyTo(byte[] target, int index)
    { 
        Buffer.BlockCopy(array, start, target, index, count);
    }

    // Other ICollection<T> members
}

Then:

List<byte> bytes = new List<byte>(new ByteArraySegment(myArray, start, count));

(Or use AddRange which has the same optimization.)

people

See more on this question at Stackoverflow