Difference between For and Foreach loop


  • Hi all,

    Can anyone tell me why dot net provide for each loop........dont tell me for each loop used for collections........we can also use for loop for collections.............and performance wise for loop is better then for each loop..............

    So, can any one tell me the actual difference between for and for each loop............

    and at where we can use for and for each loop............

    Friday, May 07, 2010 6:37 AM


All replies

  • foreach will handle everything for you and your loop will never be out of scope. Where you need to be responsible to the scope with a for loop. I don't believe there is any performance difference between the two other than it's easier to code the foreach -- less typing.

    Hope this helps.


    Friday, May 07, 2010 6:41 AM
  • foreach converts to for loop while compiler optimize the code. so it becomes an extra overhead from compiler end

    foreach loop genrally create an object also while iterating through the loop so if you can aviod that in for loop it's better

    see this for more detail http://www.codeproject.com/KB/cs/foreach.aspx

    Manish Sati
    Friday, May 07, 2010 7:12 AM
  • Thanks Jim for reply........

    but i read an article at www.codeproject.com/KB/cs/foreach.aspx which describes the performance of both loop..............that's why i become confused...............Can you tell me the right situation after reding the article.



    Friday, May 07, 2010 7:13 AM
  • then why for each loop is provided.........................

    Friday, May 07, 2010 7:26 AM
  • it may not be always true to write a efficient for loop so for ease of programming foreach loop is provided to enumerate collections which takes care of any internal optimization if required

    foreach loops are always easier, less error prone, aviod hidden errors and genrally used for objects while for loop genrally used for primitive datatypes

    few more good links

    Manish Sati
    • Marked as answer by Pratyaksh Friday, May 07, 2010 8:30 AM
    Friday, May 07, 2010 8:07 AM
  • Also, there was no mention of the value of the savings in processing time. I'm guessing that for most applications, the savings would be small enough not to notice the difference. If the time and process savings is a concern, try each and see if the savings is significant enough to make a difference, otherwise, I would take Manish's last post to heart and use foreach when ever possible.


    Friday, May 07, 2010 8:56 AM
  • For loop repeatedly execute a statement until a specified execution evaluates to false..

    for loop is maintained in condition level... it executed 0 or more times


    While for each loop repeats a group of embedded statements for each elements in a array or object collection

    For each loop gets rid of the clutter and the opportunity for error by hiding the iterator or index variable completely,comprises less coding then for loop

    • Proposed as answer by sql learner12 Tuesday, June 19, 2012 4:14 PM
    Thursday, January 05, 2012 5:38 AM