fastest way to add array elements to list

B

Bob

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);

}

Thanks,
Bob
 
J

Jon Skeet [C# MVP]

Bob said:
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);

}


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.
 
C

Cor Ligthert[MVP]

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
 
B

Ben Voigt [C++ MVP]

Jon Skeet said:
Bob said:
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);

}


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.
 
B

Bob

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

Bob said:
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);

Thanks,
Bob


Thanks so much
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top