C# ArrayList - Reverse() Method



The C# ArrayList Reverse() method is used to reverse the order of elements in the entire ArrayList or some portion of the ArrayList.

Syntax

Following are the syntax of the C# ArrayList Reverse() method −

It reverses the order of the elements in the entire ArrayList.

public virtual void Reverse();

It reverses the order of the elements in the specified range.

public virtual void Reverse (int index, int count);

Parameters

This method accepts a single parameter −

  • index: It is a zero-based starting index of the range to reverse.
  • count: It is the number of elements in the range to reverse.

Return value

This method does not return any value.

Example 1: Reverse The Entire ArrayList

Following is the basic example of the Reverse() method to reverse the order of the elements of the entire ArrayList. −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { 1, 2, 2, 3, 4 };
      
      Console.Write("Original ArrayList: ");
      foreach(var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // reverse the entire ArrayList
      arrayList.Reverse();
      
      Console.Write( "\nReversed ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Original ArrayList: 1 2 2 3 4
Reversed ArrayList: 4 3 2 2 1

Example 2: Reverse The Element's Order in Range

Let's see another example where we reverse the order of elements in a range from the starting index to the number of elements from the ArrayList using the Reverse() method −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { 1, 2, 3, 4, 5, 6, 7 };
      
      Console.Write("Initial arrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // reverse total 3 elements
      // starting from index 2
      arrayList.Reverse(2, 3);
      
      Console.Write( "\nReveresed ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Initial arrayList: 1 2 3 4 5 6 7 
Reveresed ArrayList: 1 2 5 4 3 6 7 

Example 3: Reverse the Custom Object

The below example creates a custom object and then uses the Reverse() method to reverse the order of the name in the ArrayList −

using System;
using System.Collections;

class Person
{
   public string Name { get; set; }

   public Person(string name)
   {
      Name = name;
   }

   public override string ToString()
   {
      return Name;
   }
}

class Program
{
    static void Main()
    {
       // Create an ArrayList of Person objects
       ArrayList people = new ArrayList()
       {
          new Person("Aman"),
          new Person("Kumar"),
          new Person("Gupta")
       };
    
       Console.WriteLine("Original List of People:");
       foreach (Person person in people)
          Console.Write(person + " ");
    
       people.Reverse();
    
       Console.WriteLine("\nList of People after reversing:");
       foreach (Person person in people)
          Console.Write(person + " ");
    }
}

Output

Following is the output −

Original List of People:
Aman Kumar Gupta 
List of People after reversing:
Gupta Kumar Aman 

Example 4: What if The Range Exceed the ArrayList Bound?

The below example throws an exception 'ArgumentException' if the range exceeds the bound of the ArrayList −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { 1, 2, 3, 4, 5, 6, 7 };
      
      // reverse total 5 elements
      // starting from index 3
      arrayList.Reverse(3, 5);
      
      Console.Write( "\nReveresed ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Unhandled Exception:
System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
csharp_arraylist.htm
Advertisements