Possible Duplicate:
For vs Foreach loop in C#
Is one better than another?
Seems I’ve heard that a for loop has less overhead than a foreach, but I’ve yet to see the proof of this.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
One being “better” than the other depends on your application. Are you just reading a data structure? Are you writing to a data structure? Are you not even using any sort of data structure and just doing some math?
They each have their own uses. The for each loop is usually used for reading things from a data structure (array, linked list etc). For example.
Where the for loop can be used to do the above, among other things such as update a data structure entry.