C# ArrayList - Clear() Method



The C# ArrayList Clear() method is used to removes all elements from the ArrayList, effectively resetting it to an empty state.

This method may throw a NotSupportedException if the ArrayList is read-only or has a fixed size; calling Clear will throw this exception.

Syntax

Following is the syntax of the C# ArrayList Clear() method −

public virtual void Clear ();

Parameters

This method does not accepts any parameters −

Return value

This method does not return any value.

Example 1

This is the basic example of the Clear() method to clear the ArrayList −

    
using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "A", "B", "C", "D" };
      
      Console.Write("Before Clear: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // Clear the ArrayList
      arrayList.Clear();
      
      Console.WriteLine("\nAfter Clear:");
      Console.WriteLine($"Count: {arrayList.Count}");
   }
}

Output

Following is the output −

Before Clear: A B C D 
After Clear:
Count: 0

Example 2: What If The ArrayList Sized is Fixed

Let us see another example of the Clear() method. If the ArrayList is fixed-sized then the clear method will throw a 'NotSupportedException' exception. −

using System;
using System.Collections;

class Program
{
   static void Main()
   {
      // Create a fixed-size ArrayList
      ArrayList arrayList = ArrayList.FixedSize(new ArrayList { 1, 2, 3 });

      try
      {
         arrayList.Clear();
      }
      catch (NotSupportedException ex)
      {
         Console.WriteLine($"Exception: {ex.Message}");
      }
   }
}

Output

Following is the output −

Exception: Collection was of a fixed size.

Example 3: Clearing a Dynamic List

Let us see another example of the Clear() method. Suppose we are building a dynamic menu for a user interface. After a user logs out, we may need to reset the menu options −

using System;
using System.Collections;

class Program
{
   static void Main()
   {
      // Create and populate an ArrayList representing menu items
      ArrayList menuItems = new ArrayList { "Home", "Profile", "Settings", "Logout" };

      Console.WriteLine("Menu Items Before Clear:");
      foreach (var item in menuItems)
      {
         Console.Write(item + " ");
      }

      Console.WriteLine("\nUser logs out...");
      menuItems.Clear();

      Console.WriteLine("\nMenu Items After Clear:");
      if (menuItems.Count == 0)
      {
         Console.WriteLine("No items available.");
      }
   }
}

Output

Following is the output −

Menu Items Before Clear:
Home Profile Settings Logout 
User logs out...

Menu Items After Clear:
No items available.

Example 4: Clearing a Temporary Data Cache

In this example, we use the Clear() method. Suppose we are managing a temporary cache of user input for a form. Once the user submits the form, the cache needs to be cleared −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList inputCache = new ArrayList();
      inputCache.Add("Aman kumar");
      inputCache.Add("[email protected]");
      inputCache.Add("8973454004");

      Console.WriteLine("Temporary Input Cache Before Clear:");
      foreach (var input in inputCache)
      {
         Console.WriteLine(input);
      }

      Console.WriteLine("\nForm submitted. Clearing cache...");

      // Clear the temporary input cache
      inputCache.Clear();

      // Check if the cache is cleared
      Console.WriteLine("\nTemporary Input Cache After Clear:");
      if (inputCache.Count == 0)
      {
         Console.WriteLine("Cache is empty.");
      }
      else
      {
         Console.WriteLine("Cache still has data.");
      }
   }
}

Output

Following is the output −

Temporary Input Cache Before Clear:
Aman kumar
[email protected]
8973454004

Form submitted. Clearing cache...

Temporary Input Cache After Clear:
Cache is empty.
csharp_arraylist.htm
Advertisements