C# Loop Through Arrays

C# Tutorial

C# Loop Through Arrays

In C#, you can loop through arrays to access and process each element. The most common ways are for and foreach loops.


 Using for Loop

Best when you need the index.


 


 Using foreach Loop (Recommended)

Best when you only need values, not indexes.


 


 Loop Through String Array


 


 Loop with Index and Value


 


 Loop Through 2D Array


 


 Loop Through Jagged Array


 


 Modify Array Elements Using Loop


 


 Common Mistakes

❌ Using <= instead of <
❌ Index out of range error
❌ Modifying collection inside foreach


 Summary

✔ Use for when index is needed
✔ Use foreach for simplicity
✔ Nested loops for multi-dimensional arrays
✔ Avoid index errors

You may also like...