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
How to print one dimensional array in reverse order?
In C#, there are several ways to print a one-dimensional array in reverse order. You can either reverse the actual array using Array.Reverse() or print the elements in reverse without modifying the original array.
Syntax
Using Array.Reverse() to reverse the array −
Array.Reverse(arrayName);
Using a reverse loop to print without modifying the array −
for (int i = arr.Length - 1; i >= 0; i--) {
Console.WriteLine(arr[i]);
}
Using Array.Reverse() Method
The Array.Reverse() method permanently reverses the order of elements in the array −
using System;
class Demo {
static void Main() {
int[] arr = { 76, 12, 66, 90, 34, 2, 64 };
// Display original array
Console.WriteLine("Original Array:");
foreach (int i in arr) {
Console.WriteLine(i);
}
// Reverse the array
Array.Reverse(arr);
Console.WriteLine("\nReversed Array:");
foreach (int j in arr) {
Console.WriteLine(j);
}
}
}
The output of the above code is −
Original Array: 76 12 66 90 34 2 64 Reversed Array: 64 2 34 90 66 12 76
Using Reverse Loop Without Modifying Array
To print elements in reverse order without changing the original array, use a reverse loop −
using System;
class Demo {
static void Main() {
int[] arr = { 35, 12, 66, 90, 34, 2, 64 };
Console.WriteLine("Original Array:");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i]);
}
Console.WriteLine("\nArray in Reverse Order:");
for (int i = arr.Length - 1; i >= 0; i--) {
Console.WriteLine(arr[i]);
}
Console.WriteLine("\nOriginal array unchanged:");
foreach (int element in arr) {
Console.WriteLine(element);
}
}
}
The output of the above code is −
Original Array: 35 12 66 90 34 2 64 Array in Reverse Order: 64 2 34 90 66 12 35 Original array unchanged: 35 12 66 90 34 2 64
Using LINQ Reverse() Method
You can also use LINQ's Reverse() method which creates a new sequence without modifying the original array −
using System;
using System.Linq;
class Demo {
static void Main() {
int[] arr = { 25, 45, 78, 12, 99 };
Console.WriteLine("Original Array:");
foreach (int element in arr) {
Console.WriteLine(element);
}
Console.WriteLine("\nReversed using LINQ:");
foreach (int element in arr.Reverse()) {
Console.WriteLine(element);
}
}
}
The output of the above code is −
Original Array: 25 45 78 12 99 Reversed using LINQ: 99 12 78 45 25
Comparison of Methods
| Method | Modifies Original Array | Memory Usage | Best For |
|---|---|---|---|
| Array.Reverse() | Yes | No extra memory | When you need to permanently reverse the array |
| Reverse Loop | No | No extra memory | Display only, preserving original order |
| LINQ Reverse() | No | Creates new sequence | Functional programming approach |
Conclusion
To print a one-dimensional array in reverse order, you can use Array.Reverse() to permanently reverse the array, a reverse loop to display elements without modification, or LINQ's Reverse() method for a functional approach. Choose the method based on whether you need to preserve the original array order.
