首先,设置一个数组并初始化它-
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}; //长度 Console.WriteLine("Length:" + arr.Length); for (int i = 0; i< arr.Length; i++) { Console.WriteLine(arr[i]); } } }