Reverses the sequence of the elements in a range of elements in the one-dimensional Array.
- array
- The one-dimensional Array to reverse.
- index
- The starting index of the section to reverse.
- length
- The number of elements in the section to reverse.
Type Reason ArgumentNullException array is null. RankException array is multidimensional. ArgumentOutOfRangeException index < array.GetLowerBound(0).
length < 0.
ArgumentException index and length do not specify a valid range in array (i.e. index + length > array.GetLowerBound(0) + array.Length).
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.
The Array.Reverse(Array) method can be used to reverse a jagged array.
This method is an O(n) operation, where n is length.
The following example demonstrates the Array.Reverse(Array) method.
C# Example
using System; public class ArrayReverseExample { public static void Main() { string[] strAry = { "one", "two", "three" }; Console.Write( "The elements of the array are:"); foreach( string str in strAry ) Console.Write( " {0}", str ); Array.Reverse( strAry ); Console.WriteLine(); Console.WriteLine( "After reversing the array," ); Console.Write( "the elements of the array are:"); foreach( string str in strAry ) Console.Write( " {0}", str ); } }
The output is
The elements of the array are: one two three