C# Loop, String Array

Arrow indicates movement

For and foreach can loop over string arrays. The foreach-loop has the simplest syntax, but this comes with some limitations. The for-loop uses an iteration variable, which can lead to complexity and errors.

String Array

Example

Program

First, in almost every program you have a string array, you will need to loop over the individual string elements. The simplest way of looping over the elements is with foreach, as we see here.

Foreach

Note:The code is a complete C# console program. The string array is initialized and four values are inserted into it.

Next:A foreach-loop iterates over each string in the array. The four values are printed to the console.

Console.WriteLine
Program that loops over string array: C#

using System;

class Program
{
    static void Main()
    {
	string[] arr = new string[4]; // Initialize
	arr[0] = "one";               // Element 1
	arr[1] = "two";               // Element 2
	arr[2] = "three";             // Element 3
	arr[3] = "four";              // Element 4

	// Loop over strings
	foreach (string s in arr)
	{
	    Console.WriteLine(s);
	}
    }
}

Output

one
two
three
four

Example 2

For loop

Next, we use the for-loop to accomplish the same task as we did above. The syntax is different and for-loops have some advantages. There is usually not a significant difference in performance.

For
Program that loops with for loop: C#

using System;

class Program
{
    static void Main()
    {
	string[] arr = new string[4]; // Initialize
	arr[0] = "uno";               // First element
	arr[1] = "dos";               // Second
	arr[2] = "tres";              // Third
	arr[3] = "cuatro";            // Fourth

	// Loop over strings
	for (int i = 0; i < arr.Length; i++)
	{
	    string s = arr[i];
	    Console.WriteLine(s);
	}

	// Bonus: Loop over strings backwards
	for (int i = arr.Length - 1; i >= 0; i--)
	{
	    string s = arr[i];
	    Console.WriteLine(s);
	}
    }
}

Output

uno
dos
tres
cuatro

cuatro
tres
dos
uno
Array

This example initializes a string array with four elements with Spanish numbers. Next, it loops over the string array with the for-loop construct, going from start to end. The loop starts at 0.

String type

The second loop in the above code walks over the strings backwards. This is the most efficient way to loop over a string array in reverse. It starts at the last index, which is one less than the length. It decrements.

Strings

Discussion

Note

There is not a significant performance difference between most for and foreach-loops. You can find a wealth of information on this topic. Here is a short summary of the two different looping constructs.

Foreach:Simpler syntax makes fewer typos likely. You can use custom enumerators. You can change the internal looping mechanism.

For:Usually equal in speed or faster. You can check the previous or next elements. You can loop in reverse. You can skip elements quickly.

For vs. Foreach

Summary

We saw three ways to loop over string arrays in the C# language and compared the syntax and advantages. The code to loop over your arrays is usually similar to this, except the type declarations will be different.


C#: Loop