Now I have an algorithm for dynamically allocating memory on an array:
- If array is full I create a new array of twice the size, and copy items.
- If array is one-quarter full I create a new array of half the size, and copy items.
This is fairly fast algorithm for dynamic memory allocation despite the extra over-head of copying the elements to the newly allocated array.
-
What is the faster,
List<T>or such an algorithm based on an array? What would you recommend to use? -
does
List<T>use simple array as internal data structure?
TO answer your question:
It is true, C#’s
List<T>implementation uses an internal array that isIEnumerable<T>(which means it can be LINQ Queried,foreached etc)and so on
Hence, I would ask you to use
List<T>instead of your own List.Oh and btw, if you want the source code of
List<T>from Microsoft, then here it isList.cs
EDIT
The source code of
EnsureCapacityinList<T>is: