Easy Tutorial
❮ Csharp Do While Loop Csharp Sortedlist ❯

C# Dynamic Array (ArrayList)

C# Collections

The Dynamic Array (ArrayList) represents an ordered collection of objects that can be individually indexed. It essentially serves as an alternative to an array. However, unlike arrays, you can add and remove items at a specified position using indexes, and the ArrayList will automatically resize itself. It also allows for dynamic memory allocation, addition, searching, and sorting of items within the list.

Methods and Properties of the ArrayList Class

The table below lists some commonly used properties of the ArrayList class:

Property Description
Capacity Gets or sets the number of elements that the ArrayList can contain.
Count Gets the number of elements actually contained in the ArrayList.
IsFixedSize Gets a value indicating whether the ArrayList has a fixed size.
IsReadOnly Gets a value indicating whether the ArrayList is read-only.
IsSynchronized Gets a value indicating whether access to the ArrayList is synchronized (thread safe).
Item[Int32] Gets or sets the element at the specified index.
SyncRoot Gets an object that can be used to synchronize access to the ArrayList.

The table below lists some commonly used methods of the ArrayList class:

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

Example

The following example demonstrates the concept of a dynamic array (ArrayList):

using System;
using System.Collections;

namespace ArrayListExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Creating an ArrayList
            ArrayList arrayList = new ArrayList();

            // Adding elements to ArrayList
            arrayList.Add(1);
            arrayList.Add("Two");
            arrayList.Add(3.0);

            // Displaying elements of ArrayList
            foreach (var item in arrayList)
            {
                Console.WriteLine(item);
            }

            // Removing an element
            arrayList.Remove("Two");

            // Displaying elements after removal
            Console.WriteLine("After removal:");
            foreach (var item in arrayList)
            {
                Console.WriteLine(item);
            }
        }
    }
}
using System.Collections;

namespace CollectionApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();

            Console.WriteLine("Adding some numbers:");
            al.Add(45);
            al.Add(78);
            al.Add(33);
            al.Add(56);
            al.Add(12);
            al.Add(23);
            al.Add(9);

            Console.WriteLine("Capacity: {0} ", al.Capacity);
            Console.WriteLine("Count: {0}", al.Count);

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

When the above code is compiled and executed, it produces the following result:

Adding some numbers:
Capacity: 8 
Count: 7
Content: 45 78 33 56 12 23 9 
Sorted Content: 9 12 23 33 45 56 78

C# Collection

❮ Csharp Do While Loop Csharp Sortedlist ❯