温馨提示×

c# while循环怎么读取数组

c#
小樊
83
2024-07-23 04:59:02
栏目: 编程语言

在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。通过这种方式,可以依次读取数组中的所有元素。

0