How to Reverse an Array in C# Without Using Reverse Function?

This C# tutorial explains, how to reverse an array in C# without using the reverse function. In C#, the Array class provides a convenient Reverse method that allows you to reverse an array with ease. But, in this tutorial, I will explain how to reverse an array in C# without using the Reverse function.

Reverse an array in C# without using the Reverse function

Now, let us check out different ways to reverse an array in C# without using the Reverse function.

1. Using a Loop

One of the most straightforward methods is to use a loop. By swapping the elements of the array from the start and end progressively, you can reverse its order.

using System;

namespace ArrayReversal
{
    class Program
    {
        static void Main()
        {
            string[] names = { "John", "Emily", "Michael", "Sarah", "George" };
            ReverseArray(names);

            foreach (var name in names)
            {
                Console.WriteLine(name);
            }
        }

        static void ReverseArray<T>(T[] array)
        {
            int length = array.Length;
            for (int i = 0; i < length / 2; i++)
            {
                T temp = array[i];
                array[i] = array[length - i - 1];
                array[length - i - 1] = temp;
            }
        }
    }
}

Once you run the code using visual studio, you can see the output below:

reverse array in c# without reverse function

2. Using Stacks

A stack is a Last In, First Out (LIFO) data structure. This makes it a natural choice for reversing arrays.

using System;
using System.Collections.Generic;

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

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

        static void ReverseUsingStack<T>(T[] array)
        {
            Stack<T> stack = new Stack<T>(array);
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = stack.Pop();
            }
        }
    }
}

3. Using Recursion

Though recursion is generally more memory-intensive and can be a bit harder to grasp, it offers an elegant way to reverse an array.

using System;

namespace RecursiveArrayReversal
{
    class Program
    {
        static void Main()
        {
            string[] USPresidents = { "Washington", "Adams", "Jefferson", "Madison", "Monroe" };
            ReverseRecursively(USPresidents, 0, USPresidents.Length - 1);

            foreach (var president in USPresidents)
            {
                Console.WriteLine(president);
            }
        }

        static void ReverseRecursively<T>(T[] array, int start, int end)
        {
            if (start >= end) return;

            T temp = array[start];
            array[start] = array[end];
            array[end] = temp;

            ReverseRecursively(array, start + 1, end - 1);
        }
    }
}

Conclusion

While C#’s built-in Reverse function provides an easy way to reverse arrays, understanding alternative methods can deepen your grasp of array manipulations and provide flexibility when you can’t rely on the built-in tools. Whether you’re using loops, stacks, or recursion, the key is to swap elements in the right order to achieve the desired reversal. I hope you got a complete idea of how to reverse an array in c# without reverse function.

You may like the following tutorials: