Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Linq Reverse Method
The LINQ Reverse() method in C# is used to reverse the order of elements in a sequence. It returns a new sequence with elements in reverse order without modifying the original collection.
Syntax
Following is the syntax for the LINQ Reverse() method −
public static IEnumerable<TSource> Reverse<TSource>(
this IEnumerable<TSource> source
)
Parameters
-
source: The sequence of values to reverse. It must implement
IEnumerable<T>.
Return Value
The Reverse() method returns an IEnumerable<T> whose elements correspond to those of the input sequence in reverse order.
Using Reverse() with Arrays
Here is an example showing how to reverse a character array using LINQ Reverse() method −
using System;
using System.Linq;
public class Demo {
public static void Main() {
char[] ch = { 't', 'i', 'm', 'e' };
Console.Write("Original String = ");
foreach(char arr in ch) {
Console.Write(arr);
}
var reversed = ch.Reverse();
Console.Write("\nReversed String = ");
foreach (char c in reversed) {
Console.Write(c);
}
}
}
The output of the above code is −
Original String = time Reversed String = emit
Using Reverse() with Lists
The Reverse() method works with any collection that implements IEnumerable<T> −
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> numbers = new List<int> { 10, 20, 30, 40, 50 };
Console.WriteLine("Original List:");
foreach(int num in numbers) {
Console.Write(num + " ");
}
var reversedNumbers = numbers.Reverse();
Console.WriteLine("\nReversed List:");
foreach(int num in reversedNumbers) {
Console.Write(num + " ");
}
}
}
The output of the above code is −
Original List: 10 20 30 40 50 Reversed List: 50 40 30 20 10
Using Reverse() with Strings
You can reverse characters in a string by converting it to a character array first −
using System;
using System.Linq;
public class Demo {
public static void Main() {
string text = "Hello World";
Console.WriteLine("Original String: " + text);
string reversedText = new string(text.Reverse().ToArray());
Console.WriteLine("Reversed String: " + reversedText);
}
}
The output of the above code is −
Original String: Hello World Reversed String: dlroW olleH
Key Points
-
The Reverse() method does not modify the original collection − it returns a new sequence.
-
It works with any type that implements
IEnumerable<T>. -
The method uses deferred execution − the reversal is performed when you iterate over the result.
-
For arrays, you can also use
Array.Reverse()which modifies the original array in place.
Conclusion
The LINQ Reverse() method provides an easy way to reverse the order of elements in any sequence. It returns a new reversed sequence without modifying the original collection, making it ideal for functional programming scenarios where immutability is preferred.
