J John Grandy Jan 4, 2007 #1 What is the most performant way to copy an array's elements into a List ?
M Mark Rae Jan 4, 2007 #2 John Grandy said: What is the most performant way to copy an array's elements into a List ? Click to expand... 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);
John Grandy said: What is the most performant way to copy an array's elements into a List ? Click to expand... 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);
C Chris Mullins [MVP] Jan 5, 2007 #3 John Grandy said: What is the most performant way to copy an array's elements into a List ? Click to expand... 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.
John Grandy said: What is the most performant way to copy an array's elements into a List ? Click to expand... 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.
C Chris Mullins [MVP] Jan 5, 2007 #4 Chris Mullins said: 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. Click to expand... 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 said: 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. Click to expand... 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.