C# | Reverse the order of the elements in the entire ArrayList or in the specified range - GeeksforGeeks

01 Feb, 2019
ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list. ArrayList.Reverse Method is used to reverse the order of the elements in the ArrayList in the specified range. There are two methods in the overload list of ArrayList.Reverse Method as follows:
ArrayList.Reverse()
ArrayList.Reverse(Int32, Int32)
This method is used to reverse the order of the elements in the entire ArrayList.
Syntax:
public virtual void Reverse ();

Exception: This method will give NotSupportedException if the ArrayList is read-only.
Note: This method is an O(n) operation, where n is Count.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# code to reverse the order of// the elements in the entire ArrayListusing System;using System.Collections;  class GFG {      // Driver code    public static void Main()    {          // Creating an ArrayList        ArrayList myList = new ArrayList(5);          // Adding elements to ArrayList        myList.Add("First");        myList.Add("Second");        myList.Add("Third");        myList.Add("Fourth");        myList.Add("Fifth");          // Reversing the order of elements in entire ArrayList        myList.Reverse();          // Displaying the elements in myList        for (int i = 0; i < myList.Count; i++) {            Console.WriteLine(myList[i]);        }    }}
Output:
Fifth
Fourth
Third
Second
First

Example 2:
// C# code to reverse the order of// the elements in the entire ArrayListusing System;using System.Collections;  class GFG {      // Driver code    public static void Main()    {          // Creating an ArrayList        ArrayList myList = new ArrayList(5);          // Adding elements to ArrayList        myList.Add(1);        myList.Add(2);        myList.Add(3);        myList.Add(4);        myList.Add(5);          // Reversing the order of elements in entire ArrayList        myList.Reverse();          // Displaying the elements in myList        for (int i = 0; i < myList.Count; i++) {            Console.WriteLine(myList[i]);        }    }}
Output:
5
4
3
2
1

This method is used to reverse the order of the elements in the specified range.
Syntax:
public virtual void Reverse (int index, int count);

Parameters:
index: It is the zero-based starting index of the range which is to be reversed.
count: It is the number of the elements in the range which is to be reversed.
Exceptions:
ArgumentOutOfRangeException : If the index is less than zero or count is less than zero.
ArgumentException : If index and count do not denote a valid range of elements in the ArrayList.
NotSupportedException : If the ArrayList is read-only or the ArrayList has a fixed size.
Note: This method is an O(n) operation, where n is Count.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# code to reverse a sub // range in the ArrayListusing System; using System.Collections;  class GFG {      // Driver codepublic static void Main() {           // Creating an ArrayList    ArrayList myList = new ArrayList(5);          // Adding elements to ArrayList    myList.Add("First");    myList.Add("Second");    myList.Add("Third");    myList.Add("Fourth");    myList.Add("Fifth");      // Reversing the sub-range    // starting from index 1, and     // count 3 elements    myList.Reverse(1, 3);          // Displaying the elements in myList    for (int i = 0; i < myList.Count; i++) {      Console.WriteLine(myList[i]);    }  } }
Output:
First
Fourth
Third
Second
Fifth

Example 2:
// C# code to reverse a sub // range in the ArrayListusing System; using System.Collections;  class GFG {      // Driver codepublic static void Main() {           // Creating an ArrayList    ArrayList myList = new ArrayList(5);          // Adding elements to ArrayList    myList.Add(4);    myList.Add(8);    myList.Add(12);    myList.Add(16);    myList.Add(20);      // Reversing the sub-range    // starting from index 0, and     // count 2 elements    myList.Reverse(0, 2);          // Displaying the elements in myList    for (int i = 0; i < myList.Count; i++) {      Console.WriteLine(myList[i]);    }  } }
Output:
8
4
12
16
20

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist.reverse?view=netframework-4.7.2
CSharp-Collections-ArrayList
CSharp-Collections-Namespace
CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Destructors in C#
Extension Method in C#
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
Partial Classes in C#
C# | Inheritance
C# | List Class
Difference between Hashtable and Dictionary in C#
Lambda Expressions in C#