C# Array foreach

foreach statement can be used to iterate over the elements of an Array. During each iteration, we get access to the next element.

Refer C# foreach tutorial.

Example

In the following example, we take a string array with three elements, and iterate over the elements of this array using foreach statement.

Program.cs

</>
Copy
using System;

namespace CSharpExamples {
    class Program {
        static void Main(string[] args) {
            string[] arr = {"apple", "banana", "cherry"};
            foreach (string item in arr) {
                Console.WriteLine(item);
            }
        }
    }
}

Output

apple
banana
cherry

Conclusion

In this C# Tutorial, we learned how to iterate over elements of an array using foreach statement, with examples.