C# ArrayList (With Examples)
C# ArrayList (With Examples)
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
using System.Collections;
// or
Use the Add() method or object initializer syntax to add elements in an ArrayList .
arlist1.Add(1);
arlist1.Add("Bill");
arlist1.Add(" ");
arlist1.Add(true);
arlist1.Add(4.5);
arlist1.Add(null);
};
Try it
https://www.tutorialsteacher.com/csharp/csharp-arraylist 1/7
8/27/2021 C# ArrayList (With Examples)
};
myQ.Enqueue("Hello");
myQ.Enqueue("World!");
Try it
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.
1,
"Bill",
300,
4.5f
};
https://www.tutorialsteacher.com/csharp/csharp-arraylist 2/7
8/27/2021 C# ArrayList (With Examples)
//update elements
arlist[0] = "Steve";
arlist[1] = 100;
Try it
ADVERTISEMENT
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 .
1,
"Bill",
300,
4.5F
};
Try it
Use the Insert() method to insert an element at the specified index into an ArrayList .
https://www.tutorialsteacher.com/csharp/csharp-arraylist 3/7
8/27/2021 C# ArrayList (With Examples)
1,
"Bill",
300,
4.5f
};
Console.WriteLine(val);
Try it
Use the InsertRange() method to insert a collection in an ArrayList at the specfied index.
};
};
arlist1.InsertRange(2, arlist2);
Console.Write(item + ", "); //output: 100, 200, 300, 400, 500, 600,
Try it
Use the Remove() , RemoveAt() , or RemoveRange methods to remove elements from an ArrayList .
1,
https://www.tutorialsteacher.com/csharp/csharp-arraylist 4/7
8/27/2021 C# ArrayList (With Examples)
null,
"Bill",
300,
" ",
4.5f,
300,
};
Try it
Use the Contains() method to determine whether the specified element exists in
the ArrayList or not. It returns true if exists otherwise returns false.
1,
"Bill",
300,
4.5f,
300
};
Console.WriteLine(arList.Contains(300)); // true
Console.WriteLine(arList.Contains("Bill")); // true
Console.WriteLine(arList.Contains(10)); // false
Console.WriteLine(arList.Contains("Steve")); // false
Try it
Note:
It is not recommended to use the ArrayList class due to performance issue. Instead, use List<object> to store
heterogeneous objects. To store data of same data type, use Generic List<T>.
ArrayList Class
https://www.tutorialsteacher.com/csharp/csharp-arraylist 5/7
8/27/2021 C# ArrayList (With Examples)
C# ArrayList
ArrayList Properties
Properties Description
Capacity Gets or sets the number of elements that the ArrayList can contain.
IsFixedSize Gets a value indicating whether the ArrayList has a fixed size.
ArrayList Methods
https://www.tutorialsteacher.com/csharp/csharp-arraylist 6/7
8/27/2021 C# ArrayList (With Examples)
Methods Description
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.
Contains Checks whether specified element exists in the ArrayList or not. Returns true if exists
otherwise false.
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.
https://www.tutorialsteacher.com/csharp/csharp-arraylist 7/7