C# Program to Reverse Array with Example and Explanation
Array.Reverse(array) method or by swapping elements with a loop; for example, Array.Reverse(arr); reverses the array in place.Examples
How to Think About It
Algorithm
Code
using System; class Program { static void Main() { int[] arr = {1, 2, 3, 4, 5}; Array.Reverse(arr); Console.WriteLine(string.Join(", ", arr)); } }
Dry Run
Let's trace the array [1, 2, 3, 4, 5] through the code using Array.Reverse.
Initial array
arr = [1, 2, 3, 4, 5]
Call Array.Reverse
Array.Reverse(arr) reverses the array in place.
Final array
arr = [5, 4, 3, 2, 1]
| Index | Value Before | Value After |
|---|---|---|
| 0 | 1 | 5 |
| 1 | 2 | 4 |
| 2 | 3 | 3 |
| 3 | 4 | 2 |
| 4 | 5 | 1 |
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
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)); } }
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)); } }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Array.Reverse | O(n) | O(1) | In-place reversal, best performance |
| Manual swapping | O(n) | O(1) | Learning and explicit control |
| LINQ Reverse | O(n) | O(n) | When original array must stay unchanged |
Array.Reverse for a simple and efficient way to reverse arrays in C#.Array.Reverse changes the original array and expecting a new reversed array returned.