C# Foreach: Usage & Complete Guide
In C#, the foreach statement is used to iterate through each element in a collection or array. The syntax of the foreach statement is as follows:
foreach (var item in collection)
{
// 循环体
}
The collection is the set or array to be traversed, while the item refers to the reference of the current element in each iteration.
Using the foreach statement makes it easy to iterate through elements in a collection or array without having to manually manage iterators or indexes. The foreach statement automatically handles the elements of the collection during the iteration and stops automatically after the loop.