Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to append arrays in C#?

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?

like image 422
Huck Avatar asked Nov 20 '08 09:11

Huck


People also ask

Can I append to an array in C?

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.

Which is more efficient array or List?

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.

How do you append an array to an array?

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!


1 Answers

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 :)

like image 194
Jon Skeet Avatar answered Sep 24 '22 08:09

Jon Skeet