Diferences?

by Paulo on 11/1/2007 8:10:00 AM 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


 

Re: Diferences?

by Eliyahu Goldin on 11/1/2007 5:17:00 PM I use "for" only if I need to access the iteration index "i" inside the
loop. Otherwise I use foreach. Why to introduce another variable if you are
not going to use it?

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


"Paulo" <prbspfc@uol.com.br> wrote in message
news:eOuq3hIHIHA.4112@TK2MSFTNGP05.phx.gbl...
> 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
>


 

Re: Diferences?

by bruce barker on 10/31/2007 11:44:00 PM 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)



Paulo wrote:
> 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
>
>
 

Re: Diferences?

by Paulo on 11/1/2007 8:50:00 AM The array solution is just for not foreach?

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


"bruce barker" <nospam@nospam.com> escreveu na mensagem
news:u$jG50IHIHA.4228@TK2MSFTNGP02.phx.gbl...
> 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)
>
>
>
> Paulo wrote:
>> 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


 

Re: Diferences?

by Mark Fitzpatrick on 11/1/2007 4:21:00 AM 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" <prbspfc@uol.com.br> wrote in message
news:u4BAM4IHIHA.700@TK2MSFTNGP05.phx.gbl...
> The array solution is just for not foreach?
>
> for (int i = 1; ds.tables[0].rows.count; i++)
> {
> do something
> }
>
>
> "bruce barker" <nospam@nospam.com> escreveu na mensagem
> news:u$jG50IHIHA.4228@TK2MSFTNGP02.phx.gbl...
>> 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)
>>
>>
>>
>> Paulo wrote:
>>> 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
>
>


 

Re: Diferences?

by Eliyahu Goldin on 11/1/2007 6:31:00 PM This thread shows some performance figures:
http://groups.google.com/group/microsoft.public.dotnet.languages.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" <nospam@nospam.com> wrote in message
news:u$jG50IHIHA.4228@TK2MSFTNGP02.phx.gbl...
> 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)
>
>
>
> Paulo wrote:
>> 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


 

Re: Diferences?

by Cowboy (Gregory A. Beamer) on 11/1/2007 4:44:00 AM 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. :-)

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
"Paulo" <prbspfc@uol.com.br> wrote in message
news:eOuq3hIHIHA.4112@TK2MSFTNGP05.phx.gbl...
> 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
>


 

Re: Diferences?

by Mark Rae [MVP] on 11/1/2007 3:21:00 PM "Mark Fitzpatrick" <markfitz@fitzme.com> wrote in message
news:uOp1yJJHIHA.4768@TK2MSFTNGP03.phx.gbl...

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


--
Mark Rae
ASP.NET MVP
http://www.markrae.net