How to use the Reverse() method of array class in C#


The Reverse() method in array class reverses the sequence of the elements in the entire one-dimensional Array.

To reverse an array, just use the Array.Reverse() method −

Array.Reverse(temp);

Within the reverse method, set the elements like the following code snippet.

int[] list = { 29, 15, 30, 98};
int[] temp = list;

You can try to run the following code to implement Reverse() method in C#.

Example

 Live Demo

using System;
namespace Demo {
   class MyArray {
      static void Main(string[] args) {
         int[] list = { 29, 15, 30, 98};
         int[] temp = list;
         Console.Write("Original Array: ");
         foreach (int i in list) {
            Console.Write(i + " ");
         }
         Console.WriteLine();
         // reverse the array
         Array.Reverse(temp);
         Console.Write("Reversed Array: ");
         foreach (int i in temp) {
            Console.Write(i + " ");
         }
         Console.ReadKey();
      }
   }
}

Output

Original Array: 29 15 30 98
Reversed Array: 98 30 15 29

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements