.Net framework ArrayList.Clear() method allow us to remove all elements from the ArrayList. this arraylist Clear() method exists under System.Collections namespace. this method has no required or optional parameter. the Clear() method implements as IList.Clear().
the arraylist Clear() method throw NotSupportedException exception, if the arraylist is read-only or the arraylist has a fixed size. this method set the Count is zero and references to other objects from elements of the collection are also released. but the Capacity remain unchanged in arraylist.
the following console c# example code demonstrate us how can we remove all elements from the arraylist programmatically at run time in an console application.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Program { static void Main(string[] args) { ArrayList colors = new ArrayList() { "Teal", "Plum", "Peru", "Pink" }; Console.WriteLine("ArrayList Elements...."); foreach (string color in colors) { Console.WriteLine(color); } //this line remove all elements from the ArrayList. colors.Clear(); Console.WriteLine("\nAfter Call Clear() Method; ArrayList Elements...."); foreach (string color in colors) { Console.WriteLine(color); } Console.ReadLine(); } } |
Output:
1 2 3 4 5 6 7 8 9 |
ArrayList Elements.... Teal Plum Peru Pink After Call Clear() Method; ArrayList Elements.... |