"Chris Mullins [MVP]" <cm******@yahoo.comwrote
"John Grandy" <johnagrandy-at-gmail-dot-comwrote:
>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<Twith
the correct capacity.
This means if you have a populated array myArray, you should:
List<stringmyList = 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