Array.Reverse Method (Array, Int32, Int32)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Reverses the sequence of the elements in a range of elements in the one-dimensional Array.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- array
- Type: System.Array
The one-dimensional Array to reverse.
- index
- Type: System.Int32
The starting index of the section to reverse.
- length
- Type: System.Int32
The number of elements in the section to reverse.
| Exception | Condition |
|---|---|
| ArgumentNullException | array is null. |
| RankException | array is multidimensional. |
| ArgumentOutOfRangeException | index is less than the lower bound of array. -or- length is less than zero. |
| ArgumentException | index and length do not specify a valid range in array. |
The following code example shows how to reverse the sort of the values in a range of elements in an Array.
Note: |
|---|
To run this example, see Building examples that have static TextBlock controls for Windows Phone 8. |
using System; public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // Creates and initializes a new Array. Array myArray = Array.CreateInstance(typeof(String), 9); myArray.SetValue("The", 0); myArray.SetValue("QUICK", 1); myArray.SetValue("BROWN", 2); myArray.SetValue("FOX", 3); myArray.SetValue("jumps", 4); myArray.SetValue("over", 5); myArray.SetValue("the", 6); myArray.SetValue("lazy", 7); myArray.SetValue("dog", 8); // Displays the values of the Array. outputBlock.Text += "The Array initially contains the following values:" + "\n"; PrintIndexAndValues(outputBlock, myArray); // Reverses the sort of the values of the Array. Array.Reverse(myArray, 1, 3); // Displays the values of the Array. outputBlock.Text += "After reversing:" + "\n"; PrintIndexAndValues(outputBlock, myArray); } public static void PrintIndexAndValues(System.Windows.Controls.TextBlock outputBlock, Array myArray) { for (int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++) outputBlock.Text += String.Format("\t[{0}]:\t{1}", i, myArray.GetValue(i)) + "\n"; } } /* This code produces the following output. The Array initially contains the following values: [0]: The [1]: QUICK [2]: BROWN [3]: FOX [4]: jumps [5]: over [6]: the [7]: lazy [8]: dog After reversing: [0]: The [1]: FOX [2]: BROWN [3]: QUICK [4]: jumps [5]: over [6]: the [7]: lazy [8]: dog */
Note: