This post will discuss how to reverse an array in C#.

1. Using Array.Reverse() method

To in-place reverse the order of the elements within the specified array, we can use the Array.Reverse() method. The solution works by overwriting the existing elements of the specified array without using any auxiliary array.

The following example shows how to reverse the values in an array.

Download  Run Code

2. Using Enumerable.Reverse() method

To create a reversed copy of the original array, we can use the Enumerable.Reverse() method. It just creates a new sequence with elements in the reverse order without modifying the underlying array. The following code example reverses an array using the Reverse() method.

Download  Run Code

3. Custom Routine

Another solution is to create a new array of the same type and size as the input array, fill it with elements from the source array in reverse order, and then copy the new array’s contents into the source array.

Download  Run Code

 
The above implementation requires O(n) extra space for the auxiliary array. We can avoid that by modifying the array in-place, as shown below:

Download  Run Code

That’s all about reversing an array in C#.