Removing all the elements from the List in C#

The Clear() method in C# is used to remove all elements from a List<T>. This method provides an efficient way to empty a list without having to remove elements one by one. After calling Clear(), the list's Count becomes zero, but the list object itself remains available for further operations.

Syntax

Following is the syntax for the Clear() method −

list.Clear();

Parameters

The Clear() method does not take any parameters.

Return Value

The Clear() method does not return any value. It has a void return type.

Using Clear() Method

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(String[] args) {
      List<String> list = new List<String>();
      list.Add("One");
      list.Add("Two");
      list.Add("Three");
      list.Add("Four");
      list.Add("Five");
      
      Console.WriteLine("Elements in List...");
      foreach (string res in list) {
         Console.WriteLine(res);
      }
      
      Console.WriteLine("Count of elements in list = " + list.Count);
      list.Clear();
      Console.WriteLine("Count of elements in list (after Clear) = " + list.Count);
      
      // List is still usable after Clear()
      list.Add("New Element");
      Console.WriteLine("Count after adding new element = " + list.Count);
   }
}

The output of the above code is −

Elements in List...
One
Two
Three
Four
Five
Count of elements in list = 5
Count of elements in list (after Clear) = 0
Count after adding new element = 1

Clear() vs Other Removal Methods

Method Purpose Performance
Clear() Removes all elements at once O(n) - Most efficient for removing all elements
RemoveAll() Removes elements matching a condition O(n) - Good for conditional removal
Remove() Removes first occurrence of specific element O(n) - For single element removal

Multiple Lists Example

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main(String[] args) {
      List<String> countries = new List<String>();
      countries.Add("India");
      countries.Add("US");
      countries.Add("UK");
      countries.Add("Canada");
      
      List<int> numbers = new List<int>();
      numbers.Add(10);
      numbers.Add(20);
      numbers.Add(30);
      
      Console.WriteLine("Countries count: " + countries.Count);
      Console.WriteLine("Numbers count: " + numbers.Count);
      
      // Clear both lists
      countries.Clear();
      numbers.Clear();
      
      Console.WriteLine("Countries count after Clear(): " + countries.Count);
      Console.WriteLine("Numbers count after Clear(): " + numbers.Count);
   }
}

The output of the above code is −

Countries count: 4
Numbers count: 3
Countries count after Clear(): 0
Numbers count after Clear(): 0

Conclusion

The Clear() method is the most efficient way to remove all elements from a List<T> in C#. It sets the count to zero while keeping the list object intact and ready for new elements. This method is particularly useful when you need to reset a list for reuse in your application.

Updated on: 2026-03-17T07:04:36+05:30

230 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements