In C#, List.Reverse() turns the list around: the first item becomes the last and the last becomes the first. It changes the same list in place and returns void.
There is also Reverse(index, count), which flips only a block of items and leaves the others where they are. This is different from the LINQ method of the same name, which does not change the list and instead gives you a reversed sequence to loop over. Combine Reverse with Sort() to get descending order.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.Reverse(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Call it with no arguments to flip everything, or give a start index and a count to flip a range.
C#
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Reverse(); // 5, 4, 3, 2, 1
numbers.Reverse(1, 3); // flips just 3 items from index 1
Call
What it does
numbers.Reverse()
Reverses the whole list in place.
numbers.Reverse(1, 3)
Reverses 3 items starting at index 1. The rest stay put.
numbers.AsEnumerable().Reverse()
The LINQ version. It returns a reversed sequence and does not change the list. Needs using System.Linq;.
Reverse is O(n) and uses no extra list. The range overload throws ArgumentOutOfRangeException for a negative number and ArgumentException when index + count goes past Count. Because the method returns void, you cannot write list.Reverse().ToList() on a List<T>. Compare with Array.Reverse(arr) for arrays.
Library declarations
C#
public void Reverse();
public void Reverse(int index, int count);
Parameters
Parameter
Description
index
Optional overload. The zero-based start of the range to flip.
count
Optional overload. How many items to flip, starting at index.
Return Value
Return Value
Description
void
Nothing is returned. The order inside the list is changed and Count stays the same.
C# Examples
Example 1 — Reverse the Whole List
Flip five numbers around, and flip them back.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Reverse();
Console.WriteLine(string.Join(", ", numbers));
numbers.Reverse();
Console.WriteLine(string.Join(", ", numbers));
}
}
Output
5, 4, 3, 2, 1
1, 2, 3, 4, 5
How It Works
Mirror image. The item at index 0 swaps with the item at the last index, index 1 swaps with the one before it, and so on.
In place. No new list is made. The variable numbers now holds the reversed order.
Odd middle stays. With five items, the middle item 3 swaps with itself, so it does not move.
Twice is a round trip. A second Reverse() restores the original order.
Example 2 — Reverse Only Part of a List
Flip a block of three items and leave the first and last items alone.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<char> letters = new List<char> { 'a', 'b', 'c', 'd', 'e', 'f' };
letters.Reverse(1, 3);
Console.WriteLine(string.Join("", letters));
Console.WriteLine(letters[0] + " " + letters[5]);
}
}
Output
adcbef
a f
How It Works
Index and count.Reverse(1, 3) picks the three items that begin at index 1: b, c, and d.
Only that block flips. Those three become d, c, b. The items before and after are untouched.
Join with no separator.string.Join("", letters) glues the characters into adcbef, so the change is easy to see.
Edges unchanged. Index 0 is still a and index 5 is still f. The line prints a f.
Example 3 — Descending Order and the LINQ Reverse
Sort, then reverse to get big-to-small. Compare with the LINQ version, which leaves the list as it is.
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<int> list = new List<int> { 30, 10, 20 };
IEnumerable<int> view = list.AsEnumerable().Reverse();
Console.WriteLine(string.Join(", ", view));
Console.WriteLine(string.Join(", ", list));
list.Sort();
list.Reverse();
Console.WriteLine(string.Join(", ", list));
}
}
Output
20, 10, 30
30, 10, 20
30, 20, 10
How It Works
Pick the LINQ method.AsEnumerable() makes the list look like a plain sequence, so Reverse() binds to the LINQ method, not the list method.
List untouched. The first line shows the reversed view, 20, 10, 30, and the second line shows the original list still reads 30, 10, 20.
Sort, then flip.Sort() gives 10, 20, 30, and the in-place Reverse() turns that into 30, 20, 10.
Same as a lambda.list.Sort((a, b) => b.CompareTo(a)) reaches the same descending order in one call.
Frequently Asked Questions
It reverses the order of the items in the list in place. The first item becomes the last, and the last becomes the first.
No. It returns void and changes the list you call it on. For a reversed copy, copy the list first, or use LINQ with AsEnumerable().Reverse().
Yes. Reverse(index, count) flips just count items that start at index.
Call Sort() and then Reverse(). A single Sort with a comparison lambda that swaps the sides gives the same result.
List.Reverse is an in-place method that returns void. LINQ Reverse returns a new reversed sequence and does not modify the source.