SoFunction
Updated on 2025-03-07

Detailed explanation of the use of ArrayList class in C#

1: Simple description of the ArrayList class

The dynamic array ArrayList class is in the namespace, so the namespace must be added when used, and ArrayList provides methods to add, insert or remove a range element. ArrayList represents an ordered collection of objects that can be indexed separately. It can basically replace an array. However, unlike arrays, you can add and remove items at a specified location using an index, and the dynamic array will automatically resize it. It also allows dynamic memory allocation, addition, search, and sort items in the list.

Two: ArrayList class constructor

Constructor Constructor Description
ArrayList() Initializes a new instance of the ArrayList class, which is empty and has the default initial capacity.
ArrayList(ICollection) Initializes a new instance of the ArrayList class that contains elements copied from the specified collection and has the same initial capacity as the number of elements copied.
ArrayList(Int32) Initializes a new instance of the ArrayList class, which is empty and has the specified initial capacity.

Three: Properties of ArrayList class

property Attribute description
Capacity Gets or sets the number of elements that an 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 ArrayList is read-only.
IsSynchronized Gets a value indicating whether to synchronize access to the ArrayList (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 an ArrayList.

1: Use examples

// Create an empty ArrayList and add some elements.ArrayList stringList = new ArrayList();
("a");
("abc");
("abcdef");
("abcdefg");

("Element {0} is \"{1}\"", 2, stringList[2]);
try
{
    ("Element {0} is \"{1}\"", 4, stringList[4]);
}
catch (ArgumentOutOfRangeException aoore)
{
    ("stringList({0}) is out of range.", 4);
}

Running results:

Element 2 is "abcdef"
stringList(4) is out of range.

Four: Common methods of ArrayList class

method Method Description
Add(Object) Adds the object to the end of the ArrayList.
AddRange(ICollection) Add an element of ICollection to the end of the ArrayList.
Clear() Remove all elements from ArrayList.
Clone() Create a shallow copy of ArrayList.
Contains(Object) Determines whether an element is in an ArrayList.
CopyTo(Array) Starting from the beginning of the target array, copy the entire ArrayList to a compatible one-dimensional Array.
Equals(Object) Determines whether the specified object is equal to the current object.
GetRange(Int32, Int32) Returns an ArrayList that represents a subset of elements in the source ArrayList.
IndexOf(Object) Searches for the specified Object and returns the zero-based index of the first match in the entire ArrayList.
LastIndexOf(Object) Search the specified Object throughout the ArrayList and returns the zero-based index of the last match.
Insert(Int32, Object) Inserts an element at the specified index of the ArrayList.
Remove(Object) Removes the first match of a specific object from an ArrayList.
RemoveAt(Int32) Removes the element at the specified index of ArrayList.
Reverse() Reverses the order of elements in the entire ArrayList.
Sort() Sort elements in the entire ArrayList.
ToArray() Copy the elements of the ArrayList into the new Object array.
ToString() Returns a string representing the current object.

1: Example of AddRange(ICollection) method usage

// Create and initialize a new ArrayListArrayList myAL = new ArrayList();
("ab");
("cd");
("efg");

// Create and initialize a new queue.Queue myQueue = new Queue();
("and");
("new arraylist");

// Copy the queue element to the end of the ArrayList.(myQueue);

foreach (Object obj in myAL)
{
    ("{0} ",obj);
}

Running results:

ab cd efg and new arraylist

2: () Method usage example

// Create and initialize a new ArrayListArrayList myAL = new ArrayList();
("ab");
("cd");
("efg");

//Clone the dynamic array into the new dynamic array.ArrayList newArray = (ArrayList)();

foreach (Object obj in newArray)
{
    ("{0} ",obj);
}

Running results:

ab cd efg

3: (Object) Example of use

ArrayList myAL = new ArrayList();

("ab");
("cd");
("efg");
(true);


(true);//Remove a single elementforeach (Object obj in myAL)
{
    ("{0} ", obj);
}

Running results:

ab cd efg

4: (Int32) Example of use

ArrayList myAL = new ArrayList();

("ab");
("cd");
("efg");

(1);//Delete elements according to the following numberforeach (Object obj in myAL)
{
    ("{0} ", obj);
}

Running results:

ab efg

5: (Int32) Example of use

ArrayList myAL = new ArrayList();

("ab");
("cd");
("efg");
("lm");
("nopq");

(1,3);//Delete elements according to the subscript rangeforeach (Object obj in myAL)
{
    ("{0} ", obj);
}

Running results:

ab nopq

6: () Examples of use

ArrayList myAL = new ArrayList();

("ab");
("cd");
("efg");

();//Invert all elementsforeach (Object obj in myAL)
{
    ("{0} ", obj);
}

Running results:

efg cd ab

7: (Int32, Object) Examples of use

ArrayList myAL = new ArrayList();

("ab");
("cd");
("efg");

(2,"new");
foreach (Object obj in myAL)
{
    ("{0} ", obj);
}

Running results:

ab cd new efg

8: (Int32, ICollection) Example of use

ArrayList myAL = new ArrayList();
int[] shuzu= {1,2,3,4,5};
("ab");
("cd");
("efg");

(2, shuzu);//Specify the location, insert the collectionforeach (Object obj in myAL)
{
    ("{0} ", obj);
}

Running results:

ab cd 1 2 3 4 5 efg

9: (Object) Example of use

ArrayList myAL = new ArrayList();
("ab");
("cd");
("efg");
("nopq");

bool b = ("nopq");// Whether to include the specified element("Whether it containsnopq = {0}", b);

Running results:

Whether to include nopq = true

This is the end of this article about the detailed explanation of the use of the ArrayList class in C#. For more related C# ArrayList content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!