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

CsharpProgrammingServer Side Programming

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
raja
Published on 18-Sep-2018 13:22:47
Advertisements