C# - ArrayList
In C#, the ArrayList is a non-generic collection of objects whose size increases dynamically. It is the same as Array except that its size increases dynamically.
An ArrayList can be used to add unknown data where you don't know the types and the size of the data.
Create an ArrayList
The ArrayList class included in the System.Collections namespace. Create an object of the ArrayList using the new keyword.
Adding Elements in ArrayList
Use the Add[] method or object initializer syntax to add elements in an ArrayList.
An ArrayList can contain multiple null and duplicate values.
Use the AddRange[ICollection c] method to add an entire Array, HashTable, SortedList, ArrayList, BitArray, Queue, and Stack in the ArrayList.
Accessing an ArrayList
The ArrayList class implements the IList interface. So, elements can be accessed using indexer, in the same way as an array. Index starts from zero and increases by one for each subsequent element.
An explicit casting to the appropriate types is required, or use the var variable.
Iterate an ArrayList
The ArrayList implements the ICollection interface that supports iteration of the collection types. So, use the foreach and the for loop to iterate an ArrayList. The Count property of an ArrayList returns the total number of elements in an ArrayList.
Insert Elements in ArrayList
Use the Insert[] method to insert an element at the specified index into an ArrayList.
Signature: void Insert[int index, Object value]
Use the InsertRange[] method to insert a collection in an ArrayList at the specfied index.
Signature: Void InsertRange[int index, ICollection c]
Remove Elements from ArrayList
Use the Remove[], RemoveAt[], or RemoveRange methods to remove elements from an ArrayList.
Check Element in ArrayList
Use the Contains[] method to determine whether the specified element exists in the ArrayList or not. It returns true if exists otherwise returns false.
ArrayList Class
The following diagram illustrates the ArrayList class.
ArrayList Properties
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. |
Item | Gets or sets the element at the specified index. |
ArrayList Methods
Add[]/AddRange[] | Add[] method adds single elements at the end of ArrayList. AddRange[] method adds all the elements from the specified collection into ArrayList. |
Insert[]/InsertRange[] | Insert[] method insert a single elements at the specified index in ArrayList. InsertRange[] method insert all the elements of the specified collection starting from specified index in ArrayList. |
Remove[]/RemoveRange[] | Remove[] method removes the specified element from the ArrayList. RemoveRange[] method removes a range of elements from the ArrayList. |
RemoveAt[] | Removes the element at the specified index from the ArrayList. |
Sort[] | Sorts entire elements of the ArrayList. |
Reverse[] | Reverses the order of the elements in the entire ArrayList. |
Contains | Checks whether specified element exists in the ArrayList or not. Returns true if exists otherwise false. |
Clear | Removes all the elements in ArrayList. |
CopyTo | Copies all the elements or range of elements to compitible Array. |
GetRange | Returns specified number of elements from specified index from ArrayList. |
IndexOf | Search specified element and returns zero based index if found. Returns -1 if element not found. |
ToArray | Returns compitible array from an ArrayList. |
- Difference between Array & ArrayList
- ArrayList's Methods & Properties