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 is the most efficient way to concatenate these arrays together in C# as I pull them out of the system?
An array is a contiguous block of memory and if you want to append an element, you have to write it to the position following the last occupied position, provided the array is large enough.
The array is faster in case of access to an element while List is faster in case of adding/deleting an element from the collection.
To append one array to another, use the push() method on the first array, passing it the values of the second array. The push method is used to add one or more elements to the end of an array. The method changes the contents of the original array. Copied!
You can't append to an actual array - the size of an array is fixed at creation time. Instead, use a List<T>
which can grow as it needs to.
Alternatively, keep a list of arrays, and concatenate them all only when you've grabbed everything.
See Eric Lippert's blog post on arrays for more detail and insight than I could realistically provide :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With