ArrayList Class:
a non-generic collection that can store elements of any data type.
It is part of the System.Collections namespace.
Unlike a fixed-size array, an ArrayList dynamically adjusts its capacity as
elements are added or removed.
Key characteristics
Dynamic Sizing:
It automatically expands or shrinks its capacity as needed, eliminating the need to
pre-define a fixed size.
Non-Generic:
It can store elements of different data types (heterogeneous objects) within the
same collection, as all elements are treated as Object types. This requires casting
when retrieving elements to their specific types.
Indexed Access:
Elements can be accessed, inserted, or removed using their integer index.
Common Operations:
It provides methods for adding (Add, Insert), removing (Remove, RemoveAt),
searching (Contains, IndexOf), and sorting elements.
DECLARATION:
ArrayList variablename = new ArrayList();
example: ArrayList a= new ArrayList();
Example Program:
using System;
using System.Collections;
public class Example
{
public static void Main(string[] args)
{
// Create an ArrayList
ArrayList myArrayList = new ArrayList();
// Add elements of different types
myArrayList.Add("Hello");
myArrayList.Add(123);
myArrayList.Add(true);
myArrayList.Add(3.14);
Console.WriteLine($"First element: {myArrayList[0]}");
Console.WriteLine($"Second element: {myArrayList[1]}");
Console.WriteLine("\nElements in ArrayList:");
foreach (object item in myArrayList)
{
Console.WriteLine(item);
}
myArrayList.Remove(123);
myArrayList.Insert(1, "New Item");
Console.WriteLine("\nElements after modification:");
foreach (object item in myArrayList)
{
Console.WriteLine(item);
}
}
}
ArrayList Properties
Properties 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.
Item Gets or sets the element at the specified index.
ArrayList Methods
Methods Description
Add() method adds single elements at the end of ArrayList.
Add() AddRange() method adds all the elements from the specified collection into
ArrayList.
Insert() method insert a single elements at the specified index in ArrayList.
Insert() InsertRange() method insert all the elements of the specified collection starting from
specified index in ArrayList.
Remove() method removes the specified element from the ArrayList.
Remove()
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.
Checks whether specified element exists in the ArrayList or not. Returns true if exists
Contains
otherwise false.
Clear Removes all the elements in ArrayList.
CopyTo Copies all the elements or range of elements to compitible Array.
Search specified element and returns zero based index if found. Returns -1 if element
IndexOf
not found.
ToArray Returns compitible array from an ArrayList.
Variable size arrays:
An array whose size or length is determined during runtime.
1.List<T> (Generic List):
is a generic class that provides a dynamic array-like structure.
It automatically resizes itself as elements are added or removed, handling the
underlying array management.
Example:
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Remove(10);
Console.WriteLine(numbers.Count);
2.Array.Resize<T>():
While arrays are fixed-size, the Array.Resize<T>() method allows you to create a
new array of a different size and copy the elements from the old array to the new
one.
Example:
int[] arr = new int[] { 1, 2, 3 };
Array.Resize(ref arr, 5);
arr[3] = 4;
arr[4] = 5;
Console.WriteLine(arr.Length);
Span<T> : provides a safe and efficient way to work with contiguous blocks of
memory, including arrays, without requiring an "unsafe" context for stack-allocated
arrays of variable size. This is particularly useful for performance-critical scenarios.
Code
Example:
void DeclareStackBasedArraySafe(int size)
{
Span<int> a = stackalloc int[size];
a[0] = 123;
}