In C#, List.Clear() removes all the items, so Count becomes 0. The list object itself is still there and ready to use again, like a notebook with every page erased.
The hidden array behind the list is kept, so Capacity does not shrink. That makes refilling the list cheap. If you want to give the memory back, call TrimExcess() after Clear.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.Clear(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Call Clear with no arguments on a list variable.
C#
List<int> numbers = new List<int> { 1, 2, 3 };
numbers.Clear();
// numbers.Count is 0
Call
What it does
numbers.Clear()
Removes every item. Count becomes 0.
numbers = new List<int>()
Not the same: this makes a new list. Other variables that pointed to the old list still see the old items.
numbers.Clear(); numbers.TrimExcess();
Empty the list, then release the unused room.
Clear is O(n) for item types that hold references (the list sets each slot to null so the objects can be garbage-collected) and O(1) for simple value types like int. Calling it on an already empty list is fine.
Library declarations
C#
public void Clear();
Parameters
Parameter
Description
None
Clear() takes no parameters.
Return Value
Return Value
Description
void
Nothing is returned. The list is empty afterwards: Count is 0, and Capacity is unchanged.
C# Examples
Example 1 — Clear and Reuse a List
Fill a list, clear it, show it is empty, and add to it again.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> tags = new List<string> { "c#", "list", "tutorial" };
Console.WriteLine(tags.Count);
tags.Clear();
Console.WriteLine(tags.Count);
Console.WriteLine(tags.Contains("c#"));
tags.Add("again");
Console.WriteLine(tags[0]);
}
}
Output
3
0
False
again
How It Works
Before. The list starts with three tags, so the first Count prints 3.
After Clear. All items are gone. Count is 0 and Contains("c#") is False.
Still usable. The list object was not destroyed. It is just empty, so you can call Add on it again.
Fresh index. The new item lands at index 0, because the list counts from the start again.
Example 2 — Clear Keeps the Capacity
Compare Capacity before and after. Print only a comparison, because the exact number is an internal detail.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> list = new List<int>();
for (int i = 0; i < 100; i++) {
list.Add(i);
}
int before = list.Capacity;
list.Clear();
Console.WriteLine(list.Count);
Console.WriteLine(list.Capacity == before);
Console.WriteLine(list.Capacity >= 100);
}
}
Output
0
True
True
How It Works
Grow to 100. The loop adds 100 numbers, and the list grows its hidden array as needed, so Capacity ends up at least 100.
Save the number.before stores the capacity so we can compare it. We never print the number itself.
Same room. After Clear, Count is 0 but Capacity is unchanged, so the comparison prints True.
Give it back. Call TrimExcess() after Clear if you want the list to release the unused room.
Example 3 — Clear or Make a New List?
Two variables share one list. See how Clear and new List affect the second variable differently.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> a = new List<int> { 1, 2, 3 };
List<int> b = a;
a = new List<int>();
Console.WriteLine(a.Count + " " + b.Count);
b.Clear();
Console.WriteLine(a.Count + " " + b.Count);
}
}
Output
0 3
0 0
How It Works
One list, two names. After b = a, both variables point to the list that holds 1, 2, 3.
New list for a.a = new List<int>() makes a point to a different, empty list. b still points to the old one, so the line prints 0 3.
Clear works on the object.b.Clear() empties the list that b points to, and that is the old list with three items.
Both empty. The final line shows 0 0: a has its own empty list, and b's list was just cleared.
Frequently Asked Questions
It removes all items from the list so Count is 0. The list object is still valid and can be reused.
No. Capacity stays the same. Call TrimExcess after Clear if you want to release the unused room.
Clear empties the existing list object, which every variable sharing it will see. new List<T>() creates a separate list and only changes the one variable.
No. It does nothing harmful when the list is already empty.
Yes, but it is different: Array.Clear(arr, index, length) sets array slots to their default values (0 or null) and the array length stays the same.