473,385 Members | 1,341 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,385 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 91181
"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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.