C# program to iterate over a string array with for loop

A string array in C# can be iterated using a for loop to access each element by its index. The loop runs from index 0 to the length of the array minus one, allowing you to process each string element sequentially.

Syntax

Following is the syntax for iterating over a string array with a for loop −

for (int i = 0; i < arrayName.Length; i++) {
    // Access element: arrayName[i]
}

The Length property returns the total number of elements in the array, and the loop variable i serves as the index to access each element.

Basic String Array Iteration

Create a string array and iterate through it using a for loop −

using System;

public class Demo {
    public static void Main() {
        string[] str = new string[] {
            "Videos",
            "Tutorials",
            "Tools",
            "InterviewQA"
        };
        
        Console.WriteLine("String Array...");
        for (int i = 0; i < str.Length; i++) {
            string res = str[i];
            Console.WriteLine(res);
        }
    }
}

The output of the above code is −

String Array...
Videos
Tutorials
Tools
InterviewQA

Using Index with Array Elements

You can also display the index alongside each array element −

using System;

public class Demo {
    public static void Main() {
        string[] languages = {"C#", "Java", "Python", "JavaScript"};
        
        Console.WriteLine("Programming Languages:");
        for (int i = 0; i < languages.Length; i++) {
            Console.WriteLine($"Index {i}: {languages[i]}");
        }
    }
}

The output of the above code is −

Programming Languages:
Index 0: C#
Index 1: Java
Index 2: Python
Index 3: JavaScript

Reverse Iteration

You can iterate through the string array in reverse order by starting from the last index −

using System;

public class Demo {
    public static void Main() {
        string[] colors = {"Red", "Green", "Blue", "Yellow"};
        
        Console.WriteLine("Colors in reverse order:");
        for (int i = colors.Length - 1; i >= 0; i--) {
            Console.WriteLine(colors[i]);
        }
    }
}

The output of the above code is −

Colors in reverse order:
Yellow
Blue
Green
Red

Conclusion

Using a for loop to iterate over string arrays in C# provides direct access to both the index and element values. This approach is ideal when you need to know the position of each element or when you need to iterate in a specific pattern like reverse order.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements