Back to Blog
C#

C# Array Reverse Method: Usage and Overloads

Learn how to use C#'s Array.Reverse method: its overloads, in-place behavior, range reversal, performance tradeoffs, and when LINQ's Reverse is a better fit.

Array.ReverseC# ArraysLINQ ReverseIn-Place ReversalC# Collections
Illustration of an array being reversed with the C# Array.Reverse method, showing elements swapping order.

Array.Reverse is the standard .NET method for reversing the order of elements in an array. It is a static method on the System.Array class and operates in place: it modifies the original array rather than returning a new one. Understanding its overloads and behavior is important for writing efficient and correct code.

The Array.Reverse Method and Its Overloads

The Array.Reverse method is the primary way to reverse the order of elements in an array in C#. It has several overloads:

public static void Reverse(Array array); public static void Reverse(Array array, int index, int length); public static void Reverse<T>(T[] array); public static void Reverse<T>(T[] array, int index, int length);

The first two overloads accept a non-generic Array, so they can work with any array type, including multi-dimensional arrays. The latter two are generic and type-safe for strongly typed arrays. All overloads modify the array in place and return void. If you need to preserve the original array, create a copy before calling Reverse.

Reversing a Copy Without Modifying the Original

A common requirement is to reverse an array while keeping the original intact. Since Array.Reverse works in place, you need to clone the array first. The simplest way is to use the Clone method or Array.Copy:

int[] original = { 1, 2, 3, 4, 5 }; int[] reversed = (int[])original.Clone(); Array.Reverse(reversed);

The Clone method returns a shallow copy, which is sufficient for value types and immutable reference types. For arrays of mutable objects, only the references are copied, so the elements themselves are shared. If you need a deep copy, you must handle that separately.

Reversing a Range Within an Array

The range overloads (Reverse(Array array, int index, int length) and Reverse<T>(T[] array, int index, int length)) reverse only a portion of the array. This is useful when you have a large array and need to reverse a segment without affecting the rest. The index is the zero-based starting position, and length is the number of elements to reverse.

char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f' }; Array.Reverse(letters, 1, 3); // reverses elements at indices 1, 2, 3 // letters is now { 'a', 'd', 'c', 'b', 'e', 'f' }

The range must be valid: index must be non-negative, length must be non-negative, and index + length must not exceed the array length. If the range is invalid, the method throws ArgumentOutOfRangeException or ArgumentException.

How Array.Reverse Handles Multi-Dimensional Arrays

The non-generic overloads of Array.Reverse work with multi-dimensional arrays, but they treat the array as a flat sequence of elements. The generic overloads accept only single-dimensional T[] arrays. For a two-dimensional array, the reversal happens across all elements in row-major order, not by reversing each row or column individually.

int[,] matrix = { { 1, 2 }, { 3, 4 } }; Array.Reverse(matrix); // The elements are reversed in memory order: { { 4, 3 }, { 2, 1 } }

This behavior is rarely what you want for multi-dimensional arrays. If you need to reverse rows or columns independently, iterate manually or use a jagged array (int[][]) and reverse each sub-array separately.

Performance and Memory Behavior of Array.Reverse

Array.Reverse is an in-place operation, so it does not allocate an extra array merely to hold the reversed result. The algorithm runs in O(n) time, where n is the number of elements being reversed. It swaps elements from the two ends toward the center, which is efficient for most scenarios.

For large arrays, the main cost is the swaps themselves. If the array contains reference types, only the references are swapped, not the objects. If the array contains value types, the values are copied during the swap.

One subtle performance consideration is that the non-generic overloads accept Array. If the array is accessed through an Array-typed variable, value-type elements can be boxed as they are read or written. The generic overloads avoid this and are preferred for strongly typed arrays.

Common Mistakes and Edge Cases

A frequent mistake is assuming that Array.Reverse returns a new array. Because it returns void, code like var reversed = Array.Reverse(array); will not compile. The method modifies the input array directly.

Reversing an empty array or a single-element array is valid and results in no change; the method does not throw for these cases.

If you pass null to Array.Reverse, the method throws ArgumentNullException. Check for null before calling Reverse if the array could be null.

Choosing Between Array.Reverse and LINQ's Reverse

LINQ provides a Reverse extension method for IEnumerable<T> in the System.Linq namespace, so it can also be used with arrays. Unlike Array.Reverse, the LINQ version does not modify the original sequence; it returns an IEnumerable<T> that yields elements in reverse order when enumerated.

int[] numbers = { 1, 2, 3 }; var reversed = numbers.Reverse(); // returns an IEnumerable<int>

LINQ's Reverse is deferred: no reversal happens until you iterate over the result. If you need a materialized array, you must call ToArray():

int[] reversedArray = numbers.Reverse().ToArray();

This approach allocates a new array and leaves the original unchanged. Use Array.Reverse when you want in-place reversal and can afford to modify the original. Use LINQ's Reverse when you need a non-destructive reversal or when working with other IEnumerable<T> types.

C# Array.Reverse Method: Overloads, Range Reversal, and Performance | RYUSLOG DEV