PC Review


Reply
Thread Tools Rate Thread

copy array to List

 
 
John Grandy
Guest
Posts: n/a
 
      4th Jan 2007
What is the most performant way to copy an array's elements into a List ?


 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      4th Jan 2007
"John Grandy" <johnagrandy-at-gmail-dot-com> wrote in message
news:(E-Mail Removed)...
> What is the most performant way to copy an array's elements into a List ?


string[] astrTest = new string[5];

astrTest[0] = "One";

astrTest[1] = "One";

astrTest[2] = "One";

astrTest[3] = "One";

astrTest[4] = "One";

List<string> lstTest = new List<string>(astrTest);


 
Reply With Quote
 
Chris Mullins [MVP]
Guest
Posts: n/a
 
      5th Jan 2007
"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.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, MVP C#
http://www.coversant.net/blogs/cmullins


 
Reply With Quote
 
Chris Mullins [MVP]
Guest
Posts: n/a
 
      5th Jan 2007
"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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fast copy method of sub array (=array range) possible? Thomas Lebrecht Microsoft VB .NET 0 19th Mar 2009 08:49 AM
Array.Copy and Cloning an array don't seem to work differently illegal.prime@gmail.com Microsoft Dot NET Framework 1 29th Aug 2006 12:13 AM
Array.Copy and Cloning an array don't seem to work differently illegal.prime@gmail.com Microsoft C# .NET 1 29th Aug 2006 12:13 AM
select variables ranges, copy to array, paste the array in new workbook Mathew Microsoft Excel Worksheet Functions 1 1st Apr 2005 09:40 AM
List<t> generic type - Fastest way to copy into an unmanaged array? Daniel Mori Microsoft VC .NET 2 12th Oct 2004 01:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:12 AM.