C# List.Clear() – Remove All Elements from a List

Use the List<T>.Clear() method when you want to remove every element from a C# List<T> and leave the same list empty.

Calling Clear() changes the list’s Count to 0. You do not need to remove the items one at a time or create a new list.

Syntax of C# List.Clear()

To remove or delete all the elements of a C# List, use List.Clear() function.

The definition of List.Clear() function is given below.

</>
Copy
void List<T>.Clear()

The method does not return the removed elements. It clears the contents of the existing list, so references to that list still refer to the same List<T> object.

Example – Clear a C# List and Check Its Count

In this example, we shall initialize a list and some elements to it. Then we shall call Clear() function on the list and remove all the elements from the list.

To verify if the list is empty, we shall print out length of the list.

Program.cs

</>
Copy
using System;
using System.Collections.Generic;

class Program {
    static void Main(string[] args) {
        //create list
        List<int> nums = new List<int>();
        //add elements to the list
        nums.Add(56);
        nums.Add(82);
        nums.Add(94);

        //remove all elements of nums
        nums.Clear();
        Console.WriteLine("Number of elements in the list : "+nums.Count);
    }
}

Run the above C# program.

Number of elements in the list : 0

All the elements are gone and the number of elements in the list is now zero.

The variable nums still refers to a valid list after Clear(). New elements can therefore be added to it without creating another List<int>.

Using List.Clear() Does Not Set the List to null

Clearing a list and assigning null to a list variable are different operations. Clear() keeps the list object available but removes its elements. Assigning null means the variable no longer refers to a list object.

</>
Copy
List<string> names = new List<string> { "Asha", "Ravi", "Neha" };

names.Clear();

Console.WriteLine(names.Count);
names.Add("Sara");
Console.WriteLine(names.Count);
0
1

After Clear(), names.Count is zero. The following Add() call works because names is still a valid list.

List.Clear() vs List.RemoveAll() in C#

Use Clear() when every element must be removed. If only elements matching a condition should be removed, use List<T>.RemoveAll(Predicate<T>) instead.

For example, the following code removes only the negative numbers and keeps the remaining values in the list.

</>
Copy
List<int> numbers = new List<int> { 10, -5, 20, -8, 30 };

numbers.RemoveAll(number => number < 0);

Console.WriteLine(string.Join(", ", numbers));
10, 20, 30

In short, Clear() removes all elements, while RemoveAll() removes only elements for which the supplied predicate returns true.

Checking Whether a C# List Is Empty After Clear()

The simplest way to verify that a list has been cleared is to check its Count property. A cleared list has a count of zero.

</>
Copy
items.Clear();

if (items.Count == 0)
{
    Console.WriteLine("The list is empty.");
}

When to Use List.Clear() in C#

  • Use Clear() when all items in an existing List<T> should be removed.
  • Use Remove() when you want to remove a particular value.
  • Use RemoveAt() when you want to remove the item at a particular index.
  • Use RemoveAll() when you want to remove every item that matches a condition.

C# List.Clear() Summary

In this C# Tutorial, we learned how to delete all the elements of a list in one go and make the list empty.

List<T>.Clear() removes every element from the existing list and leaves its Count at zero. The list itself remains available for reuse. When removal depends on a condition instead, RemoveAll() is the appropriate method.