Đầu tiên, đặt một mảng và khởi tạo nó -
int[] arr = new int[] {34, 56, 12}; Để lặp qua tất cả các phần tử của một mảng -
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i]);
} Hãy cho chúng tôi xem mã hoàn chỉnh -
Ví dụ
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]);
}
}
}