c# while循环怎么读取数组

avatar
作者
猴君
阅读量:0

在C#中,可以使用while循环来遍历数组。以下是一个示例代码,演示如何使用while循环读取数组中的元素:

using System;  class Program {     static void Main()     {         int[] numbers = { 1, 2, 3, 4, 5 };                  int i = 0;         while (i < numbers.Length)         {             Console.WriteLine(numbers[i]);             i++;         }     } } 

在上面的代码中,我们定义了一个整数数组numbers,然后使用while循环来遍历数组中的元素。在每次循环中,我们打印出当前索引位置的元素,并递增索引i。通过这种方式,可以依次读取数组中的所有元素。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!