ArrayList.Reverse Method
Reverses the order of the elements in the entire ArrayList.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| NotSupportedException | The ArrayList is read-only. |
This method uses Array.Reverse to reverse the order of the elements, such that the element at ArrayList [i], where i is any index within the range, moves to ArrayList [j], where j equals index + index + count - i - 1.
This method is an O(n) operation, where n is Count.
The following code example shows how to reverse the sort order of the values in an ArrayList.
using System; using System.Collections; public class SamplesArrayList { public static void Main() { // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" ); myAL.Add( "jumps" ); myAL.Add( "over" ); myAL.Add( "the" ); myAL.Add( "lazy" ); myAL.Add( "dog" ); // Displays the values of the ArrayList. Console.WriteLine( "The ArrayList initially contains the following values:" ); PrintValues( myAL ); // Reverses the sort order of the values of the ArrayList. myAL.Reverse(); // Displays the values of the ArrayList. Console.WriteLine( "After reversing:" ); PrintValues( myAL ); } public static void PrintValues( IEnumerable myList ) { foreach ( Object obj in myList ) Console.WriteLine( " {0}", obj ); Console.WriteLine(); } } /* This code produces the following output. The ArrayList initially contains the following values: The quick brown fox jumps over the lazy dog After reversing: dog lazy the over jumps fox brown quick The */
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.