Diferences?

B

bruce barker

while they have the same effect, there is a big difference.

the first case an array of objects is created, then an array indexer
used inside the loop

the second case an iterator is created at each loop. though is usually
easier to read.

the array solution is an order of magnitude faster, and is recommended
by MS for performance.


-- bruce (sqlwork.com)
 
P

Paulo

Any diferences between:

for (int i = 1; ds.tables[0].rows.count; i++)
{
do something
}

and

foreach (DataRow row in ds.Tables[0].Rows)
{
do something
}

What you recomends??????

VS 2005 asp.net C#

Thanks
 
M

Mark Fitzpatrick

Correct. This is the recommended way. The other solution relies on a much
more processor intensive way of doing things through enumeration. Although
the end result is the same, we have access to an individual item in the
collection, the way of getting there is very different.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

Paulo said:
The array solution is just for not foreach?

for (int i = 1; ds.tables[0].rows.count; i++)
{
do something
}


bruce barker said:
while they have the same effect, there is a big difference.

the first case an array of objects is created, then an array indexer used
inside the loop

the second case an iterator is created at each loop. though is usually
easier to read.

the array solution is an order of magnitude faster, and is recommended by
MS for performance.


-- bruce (sqlwork.com)


Any diferences between:

for (int i = 1; ds.tables[0].rows.count; i++)
{
do something
}

and

foreach (DataRow row in ds.Tables[0].Rows)
{
do something
}

What you recomends??????

VS 2005 asp.net C#

Thanks
 
E

Eliyahu Goldin

This thread shows some performance figures:
http://groups.google.com/group/micr...ages.csharp/browse_frm/thread/8b36227a36a15aa

Foreach has an advantage of being more readable which could be much more
important than a performance gain that no one can notice.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


bruce barker said:
while they have the same effect, there is a big difference.

the first case an array of objects is created, then an array indexer used
inside the loop

the second case an iterator is created at each loop. though is usually
easier to read.

the array solution is an order of magnitude faster, and is recommended by
MS for performance.


-- bruce (sqlwork.com)


Any diferences between:

for (int i = 1; ds.tables[0].rows.count; i++)
{
do something
}

and

foreach (DataRow row in ds.Tables[0].Rows)
{
do something
}

What you recomends??????

VS 2005 asp.net C#

Thanks
 
C

Cowboy \(Gregory A. Beamer\)

Both iterate through the objects. One does it by position in a list and the
other does so by iterating objects. The second requires a bit less code, as
you do not have to cast out the rows inside the loop.

Functionally, they are essentially equivalent, if you are actually using the
objects, which is the norm (can't think of a case where you are not). I
generally use the for (int x version personally. It is more because of
familiarity and keeping code similar than anything else. I do, at times,
iterate through objects, however.

I would suggest whichever is easier for you to get a handle on. :)
 
P

Paulo

The array solution is just for not foreach?

for (int i = 1; ds.tables[0].rows.count; i++)
{
do something
}


bruce barker said:
while they have the same effect, there is a big difference.

the first case an array of objects is created, then an array indexer used
inside the loop

the second case an iterator is created at each loop. though is usually
easier to read.

the array solution is an order of magnitude faster, and is recommended by
MS for performance.


-- bruce (sqlwork.com)


Any diferences between:

for (int i = 1; ds.tables[0].rows.count; i++)
{
do something
}

and

foreach (DataRow row in ds.Tables[0].Rows)
{
do something
}

What you recomends??????

VS 2005 asp.net C#

Thanks
 
M

Mark Rae [MVP]

The other solution relies on a much more processor intensive way of doing
things through enumeration.

Whilst that is certainly true, unless you're doing thousands and thousands
of iterations the difference will be negligible...
 
G

Guest

of course in c# 3.0 you can do the following which is the fastest and easy to
read:

ds.tables[0].rows.foreach(row->
// dosomething
);

-- bruce (sqlwork.com)
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top