System.Array.Reverse Method

Reverses the sequence of the elements in the entire one-dimensional Array.

Syntax

[System.Runtime.ConstrainedExecution.ReliabilityContract(System.Runtime.ConstrainedExecution.Consistency.MayCorruptInstance, System.Runtime.ConstrainedExecution.Cer.MayFail)]
public static void Reverse (Array array)

Parameters

array
The one-dimensional Array to reverse.

Exceptions

TypeReason
ArgumentNullException array is null.
RankException array has more than one dimension.

Remarks

After a call to this method, the element at myArray[i], where i is any index in the array, moves to myArray[j], where j equals (myArray.Length + myArray.GetLowerBound(0)) - (i - myArray.GetLowerBound(0)) - 1.

This method is an O(n) operation, where n is the Array.Length of array.

As the following example shows, the Array.Reverse(Array) method can be used to reverse a jagged array. It initializes a jagged array with one element for each month of the current year in the current culture's calendar. Each element contains an array with as many elements as that month has days. The example displays the contents of the array, calls the Array.Reverse(Array) method, and then displays the contents of the reversed array.

code reference: System.Array.Reverse#1

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0