Q&A

Q1:Are there any specific cases in which one loop is appropriate over another?
A1: Usually, when you have to walk an index or sequential set of elements (such as referencing all elements in an array), the for loop is the best choice.
Q2:Should I be concerned about the performance differences between the two types of loops?
A2: With today's fast processors, chances are good that the performance difference between the two loop types in any given situation will be overshadowed by the readability and functionality of the best choice of loop. If you have a situation in which performance is critical, write the loop using all the ways you can think of, benchmark the results, and choose the fastest loop.

Get Sams Teach Yourself C#™ in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.