What is the ArrayList class in C#?

CsharpProgrammingServer Side Programming

ArrayList class represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array.

The following are the methods of Arraylist class −

Sr.NoMethod & Description
1public virtual int Add(object value);
Adds an object to the end of the ArrayList.
2public virtual void AddRange(ICollection c);
Adds the elements of an ICollection to the end of the ArrayList.
3public virtual void Clear();
Removes all elements from the ArrayList.
4public virtual bool Contains(object item);
Determines whether an element is in the ArrayList.
5public virtual ArrayList GetRange(int index, int count);
Returns an ArrayList which represents a subset of the elements in the source ArrayList.
6public virtual int IndexOf(object);
Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it.
7public virtual void Insert(int index, object value);
Inserts an element into the ArrayList at the specified index.
8public virtual void InsertRange(int index, ICollection c);
Inserts the elements of a collection into the ArrayList at the specified index.
9public virtual void Remove(object obj);
Removes the first occurrence of a specific object from the ArrayList.
10public virtual void RemoveAt(int index);
Removes the element at the specified index of the ArrayList.
11public virtual void RemoveRange(int index, int count);
Removes a range of elements from the ArrayList.
12public virtual void Reverse();
Reverses the order of the elements in the ArrayList.
13public virtual void SetRange(int index, ICollection c);
Copies the elements of a collection over a range of elements in the ArrayList.
14public virtual void Sort();
Sorts the elements in the ArrayList.
15public virtual void TrimToSize();
Sets the capacity to the actual number of elements in the ArrayList.

Let us see an example of ArrayList −

To sort an ArrayList in C#, use the Sort() method.

The following is the ArrayList −

ArrayList arr = new ArrayList();
arr.Add(32);
arr.Add(12);
arr.Add(55);
arr.Add(8);
arr.Add(13);

Now the Sort() method is used to sort the ArrayList −

arr.Sort();

You can try to run the following code to sort an ArrayList in C# −

Example

 Live Demo

using System;
using System.Collections;

namespace Demo {
   class Program {

      static void Main(string[] args) {

         ArrayList arr = new ArrayList();

         arr.Add(89);
         arr.Add(34);
         arr.Add(77);
         arr.Add(90);

         Console.Write("List: ");
         foreach (int i in arr) {
            Console.Write(i + " ");
         }

         Console.WriteLine();
         Console.Write("Sorted List: ");
         arr.Sort();
         foreach (int i in arr) {
            Console.Write(i + " ");
         }
         Console.WriteLine();
         Console.ReadKey();
      }
   }
}

Output

List: 89 34 77 90
Sorted List: 34 77 89 90
raja
Published on 09-Aug-2018 12:09:43
Advertisements