Diferences?

Discussion in 'ASP .Net' started by Eliyahu Goldin, Nov 1, 2007.

  1. 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" <> wrote in message
    news:...
    > 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
    >
    Eliyahu Goldin, Nov 1, 2007
    #1
    1. Advertising

  2. Eliyahu Goldin

    bruce barker Guest

    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
    >
    >
    bruce barker, Nov 1, 2007
    #2
    1. Advertising

  3. Eliyahu Goldin

    Paulo Guest

    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
    Paulo, Nov 1, 2007
    #3
  4. 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" <> wrote in message
    news:...
    > The array solution is just for not foreach?
    >
    > for (int i = 1; ds.tables[0].rows.count; i++)
    > {
    > do something
    > }
    >
    >
    > "bruce barker" <> escreveu na mensagem
    > news:u$...
    >> 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

    >
    >
    Mark Fitzpatrick, Nov 1, 2007
    #4
  5. 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" <> wrote in message
    news:u$...
    > 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
    Eliyahu Goldin, Nov 1, 2007
    #5
  6. 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" <> wrote in message
    news:...
    > 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
    >
    Cowboy \(Gregory A. Beamer\), Nov 1, 2007
    #6
  7. Eliyahu Goldin

    Paulo Guest

    The array solution is just for not foreach?

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


    "bruce barker" <> escreveu na mensagem
    news:u$...
    > 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
    Paulo, Nov 1, 2007
    #7
  8. "Mark Fitzpatrick" <> wrote in message
    news:...

    > 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
    Mark Rae [MVP], Nov 1, 2007
    #8
  9. 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)


    "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
    >
    >
    >
    =?Utf-8?B?YnJ1Y2UgYmFya2VyIChzcWx3b3JrLmNvbSk=?=, Nov 1, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Enrique
    Replies:
    1
    Views:
    335
    Logan
    Dec 4, 2003
  2. Mike C. Fletcher

    Re: diferences between 22 and python 23

    Mike C. Fletcher, Dec 3, 2003, in forum: Python
    Replies:
    35
    Views:
    761
    Fredrik Lundh
    Dec 8, 2003
  3. =?ISO-8859-1?Q?G=F6ran_Andersson?=

    Diferences? Decimal (18,2) and Numeric(18,2) SQL SERVER

    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Nov 1, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    4,892
    =?Utf-8?B?bWFnaWNoeg==?=
    Nov 1, 2007
  4. nepaul
    Replies:
    3
    Views:
    159
    filipp
    Nov 6, 2012
Loading...

Share This Page