Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

จะวนซ้ำองค์ประกอบทั้งหมดของอาร์เรย์ใน C # ได้อย่างไร


ประการแรก ตั้งค่าอาร์เรย์และเริ่มต้น -

int[] arr = new int[] {34, 56, 12};

ในการวนซ้ำองค์ประกอบทั้งหมดของอาร์เรย์ -

for (int i = 0; i < arr.Length; i++) {
   Console.WriteLine(arr[i]);
}

ให้เราดูรหัสที่สมบูรณ์ -

ตัวอย่าง

using System;
public class Program {
   public static void Main() {
      int[] arr = new int[] {34, 56, 12};

      // Length
      Console.WriteLine("Length:" + arr.Length);

      for (int i = 0; i< arr.Length; i++) {
         Console.WriteLine(arr[i]);
      }
   }
}