using System; using System.Collections; class MyArrayList { public static void Main() { ArrayList list = new ArrayList(); list.Add("One"); list.Add("Two"); list.Add("Three"); Console.WriteLine("Before Clear:"); foreach (var item in list) { Console.WriteLine(item); } list.Clear(); Console.WriteLine("\nAfter Clear:"); foreach (var item in list) { Console.WriteLine(item); } } }
Before Clear: One Two Three After Clear:
using System; using System.Collections; class MyArrayList { public static void Main() { ArrayList list = new ArrayList(); list.Add(10); list.Add("Twenty"); list.Add(30.5); Console.WriteLine("Before Clear:"); foreach (var item in list) { Console.WriteLine(item); } list.Clear(); list.Add("One"); list.Add("Two"); list.Add("Three"); Console.WriteLine("\nAfter Clear:"); foreach (var item in list) { Console.WriteLine(item); } } }
Before Clear: 10 Twenty 30.5 After Clear: One Two ThreeIn this example, we create an ArrayList and add three elements of different data types. We then use the Clear method to remove all elements from the list. After that, we add three new elements to the list. The output shows that the list contains the new elements and the old elements are removed after using the Clear method. Package library: System.Collections.