The Reverse method of List<T> reverses the order all items in in the List.
The following code snippet reverses a List.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
static void Main(string[] args) { List<string> colors = new List<string>(); colors.Add("Black"); colors.Add("White"); colors.Add("Blue"); colors.Add("Red"); colors.Add("Purple"); colors.Add("Yellow"); colors.Add("Green"); Console.WriteLine("Original List items"); Console.WriteLine("==============="); // Print original order foreach (string a in colors) Console.WriteLine(a); // Reverse list items colors.Reverse(); Console.WriteLine(); Console.WriteLine("Sorted List items"); Console.WriteLine("==============="); // Print reversed items foreach (string a in colors) Console.WriteLine(a); Console.ReadKey(); } |
Output: