C# For Loop Collection: Step-by-Step Guide

One way to iterate through a collection using a for loop in C# is by following this method:

// 创建一个集合
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };

// 使用for循环遍历集合
for (int i = 0; i < numbers.Count; i++)
{
    Console.WriteLine(numbers[i]);
}

In the code example above, a List collection containing integers is initially created. Then, a for loop is used to iterate through the collection, retrieving each element and outputting it to the console.

bannerAds