0
0
CsharpProgramBeginner · 2 min read

C# Program to Reverse Array with Example and Explanation

You can reverse an array in C# using Array.Reverse(array) method or by swapping elements with a loop; for example, Array.Reverse(arr); reverses the array in place.
📋

Examples

Input[1, 2, 3, 4, 5]
Output[5, 4, 3, 2, 1]
Input[10, 20, 30]
Output[30, 20, 10]
Input[]
Output[]
🧠

How to Think About It

To reverse an array, think of flipping it so the last element becomes first and the first becomes last. You can do this by swapping the elements from the start and end moving towards the center, or simply use C#'s built-in method that does this for you.
📐

Algorithm

1
Get the input array.
2
Set two pointers: one at the start and one at the end of the array.
3
Swap the elements at these pointers.
4
Move the start pointer forward and the end pointer backward.
5
Repeat swapping until the pointers meet or cross.
6
Return or print the reversed array.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        int[] arr = {1, 2, 3, 4, 5};
        Array.Reverse(arr);
        Console.WriteLine(string.Join(", ", arr));
    }
}
Output
5, 4, 3, 2, 1
🔍

Dry Run

Let's trace the array [1, 2, 3, 4, 5] through the code using Array.Reverse.

1

Initial array

arr = [1, 2, 3, 4, 5]

2

Call Array.Reverse

Array.Reverse(arr) reverses the array in place.

3

Final array

arr = [5, 4, 3, 2, 1]

IndexValue BeforeValue After
015
124
233
342
451
💡

Why This Works

Step 1: Using Array.Reverse

The Array.Reverse method reverses the elements of the array in place, so no extra space is needed.

Step 2: Swapping elements

If done manually, swapping elements from start and end moves the array elements to their reversed positions.

Step 3: In-place reversal

Reversing in place means the original array is changed without creating a new array, saving memory.

🔄

Alternative Approaches

Manual swapping with loop
csharp
using System;
class Program {
    static void Main() {
        int[] arr = {1, 2, 3, 4, 5};
        int start = 0, end = arr.Length - 1;
        while (start < end) {
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
        Console.WriteLine(string.Join(", ", arr));
    }
}
This method shows the logic explicitly and works without built-in methods.
Using LINQ Reverse
csharp
using System;
using System.Linq;
class Program {
    static void Main() {
        int[] arr = {1, 2, 3, 4, 5};
        var reversed = arr.Reverse().ToArray();
        Console.WriteLine(string.Join(", ", reversed));
    }
}
This creates a new reversed array without modifying the original.

Complexity: O(n) time, O(1) space

Time Complexity

Reversing requires visiting each element once to swap, so it takes linear time proportional to the array size.

Space Complexity

The reversal is done in place, so no extra space is needed beyond a few variables.

Which Approach is Fastest?

Using Array.Reverse is fastest and simplest; manual swapping is similar but more verbose; LINQ creates a new array, using extra space.

ApproachTimeSpaceBest For
Array.ReverseO(n)O(1)In-place reversal, best performance
Manual swappingO(n)O(1)Learning and explicit control
LINQ ReverseO(n)O(n)When original array must stay unchanged
💡
Use Array.Reverse for a simple and efficient way to reverse arrays in C#.
⚠️
Forgetting that Array.Reverse changes the original array and expecting a new reversed array returned.