C# Array While Loop
To loop over the elements of an Array using While Loop, initialize a variable for index, increment it during each iteration, and access element at this index during each iteration.
Refer C# While Loop tutorial.
Example
In the following example, we take a string array with three elements, and iterate over the elements of this array using While Loop.
Program.cs
</>
Copy
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
string[] arr = {"apple", "banana", "cherry"};
int index = 0;
while (index < arr.Length) {
Console.WriteLine(arr[index]);
index++;
}
}
}
}
Output
apple
banana
cherry
Conclusion
In this C# Tutorial, we learned how to iterate over elements of an array using While Loop, with examples.