C# ArrayList Reverse()

Description

ArrayList Reverse() reverses the order of the elements in the entire ArrayList.

Syntax

ArrayList.Reverse() has the following syntax.


public virtual void Reverse()

Returns

ArrayList.Reverse() method returns

Example

The following code uses the Reverse() method to reverse myArrayList.


using System;/*from   www.ja  v a2 s  . c o  m*/
using System.Collections;

class MainClass
{
  public static void Main()
  {
    ArrayList myArrayList = new ArrayList();
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    

    string[] anotherStringArray = {"Here's", "some", "more", "text"};
    myArrayList.SetRange(0, anotherStringArray);

    myArrayList.Reverse();
    DisplayArrayList("myArrayList", myArrayList);

  }
  public static void DisplayArrayList(string arrayListName, ArrayList myArrayList)
  {
    for (int i = 0; i < myArrayList.Count; i++){
      Console.WriteLine(arrayListName + "[" + i + "] = " +
        myArrayList[i]);
    }
  }

}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack