473,388 Members | 1,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

copy array to List

What is the most performant way to copy an array's elements into a List ?
Jan 4 '07 #1
3 91185
"John Grandy" <johnagrandy-at-gmail-dot-comwrote in message
news:OE**************@TK2MSFTNGP04.phx.gbl...
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<stringlstTest = new List<string>(astrTest);
Jan 4 '07 #2
"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.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, MVP C#
http://www.coversant.net/blogs/cmullins
Jan 5 '07 #3
"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
Jan 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: sam | last post by:
Hi, This is a "list iterator" problem I expect it will copy the list iterator (l_iter) to the caller: eg. list<HashMap>::iterator AcctConfParser::find_Acct_rule(string i) {...
4
by: jonny | last post by:
Hey I need to search an array list to remove duplicate Data The list is populated by a search function that enters in a name if the it is told too This causes duplicate names to be entered...
1
by: Phenix Smith | last post by:
I am looking at coverting an application over to C#. Currently I have a global list varible that needs to be converted. I have tested different ideas to accomplish the same idea. Ths best way I...
13
by: Hrvoje Voda | last post by:
How to put a specified dataset table into an array list ? Hrcko
11
by: Zorpiedoman | last post by:
The problem is this: I have a list box. I set an array list as the datasource. I remove an item from the array list. I set the listbox datasource to nothing. I set the listbox datasource to...
18
by: Doug Bell | last post by:
Hi, I have a function that loads Rows from a field in a DataTable into an ArrayList. See below: It also adds a Row (eg "All Areas"). I wanted this new row to be the first record so I added a...
2
by: Brian Pelton | last post by:
I am not sure how to fix this problem I've stumbled into... I have a list<> of an interface type. I need to pass that list to a method that adds more objects to the list. But, eventually, I...
3
by: Ant | last post by:
Hi, I'm using an array list as the collection for my indexer (_alPeople). When i try to set _alpeople to a value at a certain index, I get an error. I can't simply add the value as it needs to be...
2
by: hannahs | last post by:
I'll try to explain this the best I can. I have a class called book which creates books that each hold information such as author, title, ISBN code, etc. Then I have another class called...
22
by: Energizer100 | last post by:
Hey. I'm having some difficulty in Array lists. I have to find the average of all the numbers in an array list. That's my first task. My second one is to find the mode of all the numbers, meaning the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.