How to Reverse an Array in C#? [4 Methods]

In this C# tutorial, I will explain, how to reverse an array in C# with 4 different methods.

Reverse an Array in C#

There are 4 different ways methods to reverse an array in C#.

  • Using Array.Reverse Method
  • Using a Loop
  • Using Linq
  • Using Stack

1. Using Array.Reverse Method

This is the simplest and most straightforward way to reverse an array in C#.

using System;

namespace ReverseArrayExample
{
    class Program
    {
        static void Main()
        {
            string[] cities = { "New York", "Los Angeles", "Chicago", "Houston", "Phoenix" };

            Array.Reverse(cities);

            foreach (var city in cities)
            {
                Console.WriteLine(city);
            }
        }
    }
}

Output:

Phoenix
Houston
Chicago
Los Angeles
New York

You can follow the screenshot below:

Reverse an Array in C#

2. Using a Loop

Another method is to manually reverse the array by swapping elements using a loop in C#. Below is the complete code to reverse the array in c# using for loop.

using System;

namespace ReverseArrayExample
{
    class Program
    {
        static void Main()
        {
            string[] cities = { "New York", "Los Angeles", "Chicago", "Houston", "Phoenix" };

            int len = cities.Length;
            for (int i = 0; i < len / 2; i++)
            {
                string temp = cities[i];
                cities[i] = cities[len - 1 - i];
                cities[len - 1 - i] = temp;
            }

            foreach (var city in cities)
            {
                Console.WriteLine(city);
            }
        }
    }
}

This is how to reverse array in c# using for loop.

3. Using Linq

For those who prefer functional programming or LINQ style methods, this is an elegant way to reverse an array in C#.

using System;
using System.Linq;

namespace ReverseArrayExample
{
    class Program
    {
        static void Main()
        {
            string[] cities = { "New York", "Los Angeles", "Chicago", "Houston", "Phoenix" };

            cities = cities.Reverse().ToArray();

            foreach (var city in cities)
            {
                Console.WriteLine(city);
            }
        }
    }
}

4. Using Stack

The nature of the stack is “Last In, First Out” (LIFO). We can utilize this feature to reverse an array in C#.

using System;
using System.Collections.Generic;

namespace ReverseArrayExample
{
    class Program
    {
        static void Main()
        {
            string[] cities = { "New York", "Los Angeles", "Chicago", "Houston", "Phoenix" };

            Stack<string> stack = new Stack<string>(cities);
            cities = stack.ToArray();

            foreach (var city in cities)
            {
                Console.WriteLine(city);
            }
        }
    }
}

You can check the output below:

How to Reverse an Array in C#

These are the 4 methods you can use to reverse an array in C#.

Conclusion

Reversing an array in C# can be achieved in multiple ways, each with its own use cases and advantages. Whether you’re looking for a straightforward method using built-in libraries, a functional approach with LINQ, or a hands-on approach with loops or stacks, C# provides the tools you need. I hope you got a complete idea of how to reverse an array in C#.

You may also like: