C# Array - Reverse() Method



The C# Array Reverse() method is used to reverse the order of the elements in a one-dimensional array or in a range (portion) of the array.

Exception

There are the following exceptions of Reverse() methods −

  • ArgumentNullException: If array is null.
  • RankException: If array is multidimensional.

Syntax

Following is the syntax of the C# Array Reverse() method −

public static void Reverse (Array array, int index, int length);

Parameters

This method accepts the following parameters −

  • array: An one-dimensional array to reverse.
  • index: The starting index of the portion to reverse
  • length: The number of element in the portion to reverse.

Return value

This method does not return any value.

Example 1: Reverse the Integer Array

Let us crate a basic example of the Reverse() method to reverse the order of the elements in an array −

    
using System;
class Program {
   static void Main() {
      int[] numbers = { 1, 2, 3, 4, 5 };
      // Reverse the array
      Array.Reverse(numbers);
      Console.WriteLine(string.Join(", ", numbers));
   }
}

Output

Following is the output −

5, 4, 3, 2, 1

Example 2: Reverse the String Array

Let us see another example of the Reverse() method to reverse the order of elements of the string array −

using System;
class Program {
   static void Main() {
      string[] words = { "apple", "banana", "cherry", "date", "elderberry" };
      Array.Reverse(words);
      Console.WriteLine(string.Join(", ", words));
   }
}

Output

Following is the output −

elderberry, date, cherry, banana, apple

Example 3: Reverse in a Portion

This is another, example of the Reverse() method. Here, we use this method to Reverse an integer array in a portion −

using System;
class Program {
   static void Main() {
      int[] numbers = { 1, 2, 3, 4, 5 };
      
      // Reverse a valid portion of the array (e.g., starting at index 2, length 3)
      Array.Reverse(numbers, 2, 3);
      Console.WriteLine(string.Join(", ", numbers));
   }
}

Output

Following is the output −

1, 2, 5, 4, 3

Example 4: Reverse a Portion of a String Array

Here, in this example we uses the Reverse() method to reverse the order of elements in a portion in string array or reverse the entire string array −

using System;
class Program {
   static void Main() {
       string[] fruits = { "apple", "banana", "cherry", "date", "elderberry", "fig" };
    
       Console.WriteLine("Original Array:");
       Console.WriteLine(string.Join(", ", fruits));
    
       // Reverse a portion of the array
       Array.Reverse(fruits, 1, 3);
    
       Console.WriteLine("\nArray After Partial Reverse:");
       Console.WriteLine(string.Join(", ", fruits));
    
       // Reverse the entire string array
       Array.Reverse(fruits);
    
       Console.WriteLine("\nArray After Full Reverse:");
       Console.WriteLine(string.Join(", ", fruits));
   }
}

Output

Following is the output −

Original Array:
apple, banana, cherry, date, elderberry, fig
    
Array After Partial Reverse:
apple, date, cherry, banana, elderberry, fig
    
Array After Full Reverse:
fig, elderberry, banana, cherry, date, apple
csharp_array_class.htm
Advertisements