[Solved] C# Print an array of strings in the reverse order


Here is one option:

static void Main(string[] args)
{
    int num = 0;
    string[] names = { "Alice", "Bob", "Charles", "Delilah" };

    while (num < names.Length)
    {
        Console.WriteLine(names.Reverse().ElementAt(num));
        num++;
    }
    
    // Prints
    //
    // Delilah
    // Charles
    // Bob
    // Alice
    Console.ReadKey();
}

As per the comments it is not optimal to call Reverse() in each loop. Instead, perform the Reverse() outside of the loop.

A better alternative might be:

static void Main(string[] args)
{
    string[] names = { "Alice", "Bob", "Charles", "Delilah" };

    for (int num = names.Length - 1; num >= 0; num--)
    {
        Console.WriteLine(names[num]);
    }

    Console.ReadKey();
}

5

solved C# Print an array of strings in the reverse order