Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

fastest way to add array elements to list

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob - 28 Aug 2007 23:47 GMT
Is there a faster way to add the array elements to a list then looping
through
the elements like:

double[] toadd;
list<double> listwant;

for (int i=0;i<toadd.Length;i++){
listwant.Add(toadd[i]);

}

Thanks,
Bob
Jon Skeet [C# MVP] - 29 Aug 2007 00:17 GMT
> Is there a faster way to add the array elements to a list then looping
> through
[quoted text clipped - 7 lines]
>
> }

In this particular case, yes:

List<double> listwant = new List<double>(toadd);

Whether it applies to your real life situation is a different matter,
however.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Ben Voigt [C++ MVP] - 30 Aug 2007 20:46 GMT
>> Is there a faster way to add the array elements to a list then looping
>> through
[quoted text clipped - 11 lines]
>
> List<double> listwant = new List<double>(toadd);

or List<T>.AddRange(IEnumerable<T>) can accept an array as well, in case the
list isn't empty to begin with.

> Whether it applies to your real life situation is a different matter,
> however.
Cor Ligthert[MVP] - 29 Aug 2007 04:43 GMT
Bob,

This kind of questions depends mostly for what you mean with "fast".

If it is about looping, then it is mostly that in a method the looping is
replaced behind the scene and the processing time will be almost the same.
However, often it can with less row to write, which does not mean that it
goes faster.

Remember that looping in a good written loop is extremely fast, at that
point you will seldom find any processing time that is recognizable by the
user.

However if it is about coding, than you sometimes can find some less rows,
although with a foreach loop we are talking as well mostly about 2 or 3
rows.

Cor

> Is there a faster way to add the array elements to a list then looping
> through
[quoted text clipped - 10 lines]
> Thanks,
> Bob
Bob - 31 Aug 2007 04:28 GMT
On Aug 28, 10:43 pm, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
wrote:
> Bob,
>
[quoted text clipped - 29 lines]
> > Thanks,
> > Bob

Thanks so much

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.