Languages
[Edit]
EN

C# / .NET - iterate over array of strings

3 points
Created by:
Roseanne-Read
1365

In this article, we would like to show you how to iterate over array of strings in C#.

Quick solution:

string[] letters = { "A", "B", "C"};

foreach (string letter in letters)
{
    Console.WriteLine(letter);
}

or:

string[] letters = { "A", "B", "C" };

for (int i = 0; i < letters.Length; i++)
{
    Console.WriteLine(letters[i]);
}

 

1. Practical example using foreach loop

In this section, we present full example of how to use foreach loop to iterate over array of strings. 

using System;

public class TestClass
{
    public static void Main()
    {

        string[] letters = { "A", "B", "C"};

        foreach (string letter in letters)
        {
            Console.WriteLine(letter);
        }
    }
}

Output:

A
B
C

2. Using for loop

In this example, we use simple for loop to iterate over array of strings.

using System;

public class TestClass
{
    public static void Main()
    {

        string[] letters = { "A", "B", "C" };

        for (int i = 0; i < letters.Length; i++)
        {
            Console.WriteLine(letters[i]);
        }
    }
}

Output:

A
B
C
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join