"Chris Mullins [MVP]" <(E-Mail Removed)> wrote
> "John Grandy" <johnagrandy-at-gmail-dot-com> wrote:
>> What is the most performant way to copy an array's elements into a List ?
>>
>
> I always use the .AddRange() method on List<T>. I've never profiled it to
> see if there's a faster way, but I would think not.
>
> Certainly, this is the best method in terms of Code Clarity and
> Maintenance, which are generally way more important than the actual
> performance.
I should add that, if it's practical, you should allocate your List<T> with
the correct capacity.
This means if you have a populated array myArray, you should:
List<string> myList = new List<string>(myArray.Length);
myList.AddRange(myArray);
Doing this will avoid all sorts of unnecessary memory allocations and array
resizes.
--
Chris Mullins, MCSD.NET, MCPD:Enterprise, MVP C#
http://www.coversant.net/blogs/cmullins