Home »
.Net »
C# Programs
C# - Reverse an Array Using Array.Reverse() Method
Here, we are going to learn how to reverse an integer array using a predefined method in C#?
Submitted by Nidhi, on September 03, 2020 [Last updated : March 19, 2023]
Problem statement
Given an array, we have to reverse an integer array using a predefined method in C#.
Reversing an array
To reverse an array in C#, you can use Array.reverse() method, this method is a predefined method of Array class and reverse the elements of this array (i.e., a reverse array of this array).
Note
The Array.reverse() method does not return reverse array. The method is called with this array and reverse its elements.
Algorithm/Steps
The steps to reverse an array in C# are:
- Create an array of integers (or any kind of type).
- Assign the array with some values or input from the user.
- Call the reverse() method with this array i.e., the array whose
- elements you want to reverse.
- Print the array elements.
C# program to reverse an array using Array.Reverse() method
The source code to reverse an integer array using the predefine method is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# program to reverse an array
//using the predefined method.
using System;
class Sample
{
public static void Main()
{
int[] intArr = new int[5]{10,20,30,40,50};
int loop = 0;
Array.Reverse(intArr);
Console.WriteLine("Array after Reverse() method: ");
for (loop = 0; loop < intArr.Length; loop++)
{
Console.WriteLine("Element "+(loop+1)+" is "+intArr[loop]);
}
}
}
Output
Array after Reverse() method:
Element 1 is 50
Element 2 is 40
Element 3 is 30
Element 4 is 20
Element 5 is 10
Press any key to continue . . .
Explanation
In the above program, we created a Sample class that contains the Main() method. In the Main() method, we created an array of integers that contains 5 elements.
Array.Reverse(intArr);
Here we reversed the elements of array "intArr" using the Reverse() method of Array class and then print the reversed array on the console screen.
C# Basic Programs »