Possible Duplicate:
For vs Foreach loop in C#
What is the major difference between ‘for each’ and ‘for’ loops in .NET? Is there any performance gain while comparing these two? Which one gives a better performance/faster/memory management?
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.
See The Code Project article foreach vs. for (C#)*.
foreachis thinking about everything as a collection and is treating them as a collection. That will also reduce the performance of the work.To write high performance code that is not for collections, use a
forloop.Even for collections,
foreachmay look handy when using, but it’s not that efficient. Therefore, I strongly recommend everyone to use aforloop rather thanforeachat any stage.