在C#中,可以使用foreach循環(huán)來遍歷List中的元素。例如:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}
這段代碼會輸出List中的每一個元素。當(dāng)然,你也可以使用for循環(huán)來遍歷List:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
for (int i = 0; i < numbers.Count; i++)
{
Console.WriteLine(numbers[i]);
}
以上兩種方法都可以實現(xiàn)循環(huán)遍歷List中的元素。