For vs Foreach in C#: Key Differences and When to Use Each
for loops use an index to iterate and offer more control, while foreach loops simplify iterating over collections without manual indexing. Use foreach for cleaner code with collections and for when you need index access or to modify elements.Quick Comparison
Here is a quick side-by-side comparison of for and foreach loops in C#.
| Aspect | for loop | foreach loop |
|---|---|---|
| Syntax | Uses index variable and condition | Iterates directly over collection elements |
| Control | Full control over index and iteration | No index access, read-only iteration |
| Use case | When index or element modification needed | When just reading elements in collection |
| Performance | Slightly faster for arrays due to indexing | Simpler but may be slower on some collections |
| Safety | Risk of index errors | Safer, no index errors possible |
| Supported types | Works with any indexable collection | Works with any IEnumerable collection |
Key Differences
The for loop in C# requires you to manage an index variable manually. You set a start point, an end condition, and how the index changes each time. This gives you full control to access elements by their position and even modify them if the collection allows.
In contrast, the foreach loop automatically goes through each element in a collection without exposing the index. It works with any collection that implements IEnumerable. This makes your code simpler and less error-prone because you don't have to worry about going out of bounds or managing the loop counter.
However, foreach does not allow modifying the collection elements directly if they are value types, and you cannot skip or jump to specific elements easily. Also, for loops can be slightly faster for arrays because they use direct indexing, but for most cases, the difference is minimal.
Code Comparison
Here is how you use a for loop to print all elements of an integer array:
int[] numbers = { 10, 20, 30, 40, 50 }; for (int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]); }
Foreach Equivalent
The equivalent foreach loop to print the same array elements looks like this:
int[] numbers = { 10, 20, 30, 40, 50 }; foreach (int number in numbers) { Console.WriteLine(number); }
When to Use Which
Choose for loops when you need to access elements by their index, modify elements in place, or control the iteration process precisely (like skipping or reversing). Use foreach loops when you want clean, readable code to simply read or process each element in a collection without worrying about indexes or loop counters.
In general, prefer foreach for collections and for for arrays or when index control is necessary.
Key Takeaways
foreach for simple, safe iteration over collections without index management.for when you need index access or to modify elements during iteration.foreach works with any IEnumerable, while for requires indexable collections.for loops offer more control but require careful index handling to avoid errors.