溫馨提示×

c#中for循環如何遍歷數組

c#
小樊
144
2024-09-20 06:20:58
欄目: 編程語言

在C#中,你可以使用for循環或者foreach循環來遍歷數組。下面是兩種方法的示例:

  1. 使用for循環遍歷數組:
int[] array = new int[] { 1, 2, 3, 4, 5 };

for (int i = 0; i < array.Length; i++)
{
    Console.WriteLine("Element at index " + i + ": " + array[i]);
}
  1. 使用foreach循環遍歷數組:
int[] array = new int[] { 1, 2, 3, 4, 5 };

foreach (int element in array)
{
    Console.WriteLine("Element: " + element);
}

在這兩個示例中,我們都遍歷了一個整數數組,并打印出了每個元素。for循環使用索引來訪問數組的元素,而foreach循環則直接訪問數組的元素。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女