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# Program to invert the order of elements in a sequence
In C#, you can invert the order of elements in a sequence using several built-in methods. The most common approaches include using Queryable.Reverse() with LINQ, Array.Reverse(), and Enumerable.Reverse(). Each method has its specific use cases and performance characteristics.
Syntax
Following is the syntax for using Queryable.Reverse() method −
IQueryable<T> result = sequence.AsQueryable().Reverse();
Following is the syntax for using Array.Reverse() method −
Array.Reverse(array);
Following is the syntax for using Enumerable.Reverse() method −
IEnumerable<T> result = sequence.Reverse();
Using Queryable.Reverse() Method
The Queryable.Reverse() method returns an IQueryable<T> that represents the sequence with elements in reverse order −
using System;
using System.Linq;
public class Demo {
public static void Main() {
char[] ch = { 'd', 'r', 'i', 'v', 'e' };
Console.Write("Original String = ");
foreach(char arr in ch)
Console.Write(arr);
IQueryable<char> res = ch.AsQueryable().Reverse();
Console.Write("\nReversed String = ");
foreach (char c in res)
Console.Write(c);
}
}
The output of the above code is −
Original String = drive Reversed String = evird
Using Array.Reverse() Method
The Array.Reverse() method reverses the array in-place, modifying the original array −
using System;
public class Demo {
public static void Main() {
int[] numbers = { 10, 20, 30, 40, 50 };
Console.Write("Original Array: ");
foreach(int num in numbers)
Console.Write(num + " ");
Array.Reverse(numbers);
Console.Write("\nReversed Array: ");
foreach(int num in numbers)
Console.Write(num + " ");
}
}
The output of the above code is −
Original Array: 10 20 30 40 50 Reversed Array: 50 40 30 20 10
Using Enumerable.Reverse() Method
The Enumerable.Reverse() method creates a new sequence with elements in reverse order without modifying the original sequence −
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<string> words = new List<string> { "Hello", "World", "C#", "LINQ" };
Console.Write("Original List: ");
foreach(string word in words)
Console.Write(word + " ");
var reversed = words.Reverse();
Console.Write("\nReversed List: ");
foreach(string word in reversed)
Console.Write(word + " ");
Console.Write("\nOriginal List (unchanged): ");
foreach(string word in words)
Console.Write(word + " ");
}
}
The output of the above code is −
Original List: Hello World C# LINQ Reversed List: LINQ C# World Hello Original List (unchanged): Hello World C# LINQ
Comparison of Methods
| Method | Modifies Original | Return Type | Performance |
|---|---|---|---|
| Array.Reverse() | Yes | void | Fastest (in-place) |
| Enumerable.Reverse() | No | IEnumerable<T> | Good (deferred execution) |
| Queryable.Reverse() | No | IQueryable<T> | For query providers |
Conclusion
C# provides multiple methods to reverse sequences: Array.Reverse() for in-place reversal of arrays, Enumerable.Reverse() for creating new reversed sequences, and Queryable.Reverse() for LINQ query providers. Choose the method based on whether you need to preserve the original sequence and your performance requirements.
