Class System.Collections.ArrayList
Description
Represents a strongly typed list of objects that can be accessed by index. Provides methods for manipulating the list, such as searching and sorting. The ArrayList
class is a non-generic collection that can hold any type of object. It is recommended to use the generic List<T>
class when possible, as it provides better type safety and performance.
Syntax
[System.ObsoleteAttribute("ArrayList is collections are obsolete. The System.Collections.Generic collection types are recommended instead. http://go.microsoft.com/fwlink/?linkid=140926", true)]
public class ArrayList : System.Collections.IList, System.Collections.ICollection, System.Collections.IEnumerable, System.Collections.ICloneable
Members
The ArrayList
class includes the following members:
Member | Description |
---|---|
ArrayList() | Initializes a new instance of the ArrayList class that is empty, has the default initial capacity, and uses the default equality comparer for the element type. |
Add(Object) | Adds an object to the end of the ArrayList . |
Clear() | Removes all elements from the ArrayList . |
Contains(Object) | Determines whether an element is in the ArrayList . |
IndexOf(Object) | Searches for the specified object and returns the zero-based index of the first occurrence within the entire ArrayList . |
Remove(Object) | Removes the first occurrence of a specific object from the ArrayList . |
Count | Gets the number of elements contained in the ArrayList . |
this[int index] | Gets or sets the element at the specified index. |
Remarks
ArrayList
is a dynamic array that can grow or shrink as needed. It is part of the older System.Collections
namespace and is generally less efficient and type-safe than its generic counterpart, List<T>
. For new development, prefer List<T>
for strongly typed collections.
The ArrayList
is not thread-safe. If you require thread-safe access to the collection, consider using a synchronized wrapper or other thread-safe collection types.
Constructors
ArrayList()
Syntax
public ArrayList()
Initializes a new instance of the ArrayList
class that is empty, has the default initial capacity, and uses the default equality comparer for the element type.
Methods
Add(Object)
Syntax
public virtual int Add(object value)
Adds an object to the end of the ArrayList
.
Parameters
Name | Type | Description |
---|---|---|
value |
object |
The object to be added to the end of the ArrayList . The value can be null . |
Return Value
int |
The zero-based index at which the object was added. |
Clear()
Syntax
public virtual void Clear()
Removes all elements from the ArrayList
.
Contains(Object)
Syntax
public virtual bool Contains(object value)
Determines whether an element is in the ArrayList
.
Parameters
Name | Type | Description |
---|---|---|
value |
object |
The object to locate in the ArrayList . The value can be null . |
Return Value
bool |
true if the object is found in the ArrayList ; otherwise, false . |
IndexOf(Object)
Syntax
public virtual int IndexOf(object value)
Searches for the specified object and returns the zero-based index of the first occurrence within the entire ArrayList
.
Parameters
Name | Type | Description |
---|---|---|
value |
object |
The object to search for. The value can be null . |
Return Value
int |
The zero-based index of the first occurrence of value within the entire ArrayList , if found; otherwise, -1. |
Remove(Object)
Syntax
public virtual void Remove(object value)
Removes the first occurrence of a specific object from the ArrayList
.
Parameters
Name | Type | Description |
---|---|---|
value |
object |
The object to remove from the ArrayList . The value can be null . |
Properties
Count
Syntax
public virtual int Count { get; }
Gets the number of elements contained in the ArrayList
.
Return Value
int |
The number of elements contained in the ArrayList . |
this[int index]
Syntax
public virtual object this[int index] { get; set; }
Gets or sets the element at the specified index.
Parameters
Name | Type | Description |
---|---|---|
index |
int |
The zero-based index of the element to get or set. |
Return Value
object |
The element at the specified index. |
Example
The following example demonstrates how to create an ArrayList
, add elements to it, and access elements by index.
using System;
using System.Collections;
public class ArrayListExample
{
public static void Main(string[] args)
{
// Create an ArrayList
ArrayList myList = new ArrayList();
// Add elements
myList.Add("Apple");
myList.Add(123);
myList.Add(true);
Console.WriteLine($"Number of elements: {myList.Count}");
// Access elements by index
Console.WriteLine($"Element at index 0: {myList[0]}");
Console.WriteLine($"Element at index 1: {myList[1]}");
// Check if an element exists
if (myList.Contains("Apple"))
{
Console.WriteLine("The list contains 'Apple'.");
}
// Remove an element
myList.Remove(123);
Console.WriteLine($"Number of elements after removing 123: {myList.Count}");
// Iterate through the list
Console.WriteLine("All elements:");
foreach (object item in myList)
{
Console.WriteLine(item);
}
}
}