The foreach loop in C# is used to iterate over a collection of items, such as
arrays, lists, or other enumerable
collections. It is beneficial for iterating through elements without knowing the
collection size or managing loop counters. The foreach loop provides a cleaner and more readable way to traverse collections than the traditional for loop.
Here is the basic syntax of a foreach loop in C#:
foreach (var item in collection)
{
// Code to execute for each item
}
Example Usage
Iterating Over an Array:
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Iterating Over a List:
List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
foreach (string name in names)
{
Console.WriteLine(name);
}
Key Points
The foreach loop can only be used with collections that implement the
IEnumerable interface.
The loop variable (e.g., item in the examples) is read-only within the scope of the loop. You cannot assign a new value to
item within the loop body.
The foreach loop automatically handles the iteration bounds, so you do not need to manage index variables manually.
It is generally safer and less error-prone than using a for loop with index variables, especially when working with collections that can change in size or when the iteration logic is complex.
Limitations
You cannot modify the collection itself (e.g., adding or
removing elements) while iterating over it using a foreach loop. Doing so will result in an
InvalidOperationException.
If you need to modify elements of a collection, consider using a for loop or other appropriate methods that provide access to the collection by index.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The foreach loop in C# is used to iterate over a collection of items, such as arrays, lists, or other enumerable collections. It is beneficial for iterating through elements without knowing the collection size or managing loop counters. The foreach loop provides a cleaner and more readable way to traverse collections than the traditional for loop.
Here is the basic syntax of a foreach loop in C#:
Example Usage
Iterating Over an Array:
Iterating Over a List:
Key Points
foreachloop can only be used with collections that implement theIEnumerableinterface.itemin the examples) is read-only within the scope of the loop. You cannot assign a new value toitemwithin the loop body.foreachloop automatically handles the iteration bounds, so you do not need to manage index variables manually.forloop with index variables, especially when working with collections that can change in size or when the iteration logic is complex.Limitations
foreachloop. Doing so will result in anInvalidOperationException.forloop or other appropriate methods that provide access to the collection by index.