How to empty a list in C?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want to empty a List<T> in C#, the usual answer is Clear(). That removes all items while keeping the list object itself alive, which is exactly what most code wants. The details start to matter when other parts of the program hold a reference to the same list, or when you also care about capacity and memory reuse.
The Standard Approach: Clear()
List<T>.Clear() removes every element from the list and sets Count to zero. The list instance is still the same object, so any code that already references it continues to see the cleared list.
Output:
This is the idiomatic approach for most applications. It is readable, efficient, and communicates intent immediately.
Why Reassigning Is Not the Same Thing
Sometimes developers write numbers = new List<int>() and think they have "cleared the list". That only changes one variable to point at a different list. Any other reference still points to the original collection.
Output:
If shared references matter, use Clear(). Reassignment is only appropriate when you truly want a brand new collection for the current variable.
Capacity and Memory Behavior
Clear() removes the elements, but it does not necessarily shrink the internal array immediately. That is usually good for performance because the list can be refilled without repeated allocations.
If you want to release extra storage after clearing a large list, call TrimExcess() or create a new list when that tradeoff makes sense.
Be careful here. Shrinking capacity aggressively can hurt performance if the list will soon grow again.
Removing Items One by One
You can technically empty a list by removing each item individually, but it is usually the wrong tool.
This works, but it is less expressive than Clear(). It is only useful when you need per-item side effects while removing data.
Another option is RemoveAll with a predicate:
That empties the list, but it is more indirect than Clear() and communicates less clearly to the reader.
Choosing the Right Pattern
Use Clear() when you want to reuse the same list instance and keep references consistent. Use reassignment when the variable should point to a completely new list and no shared references need updating. Use per-item removal only when logic must run during deletion.
The choice is often architectural, not just syntactic. In UI state containers, caches, and service objects, keeping the same list instance can be important because observers or consumers may be holding onto it.
Common Pitfalls
One common mistake is assuming Clear() frees all memory immediately. It removes element references, which helps garbage collection, but the internal capacity may remain allocated for reuse.
Another issue is using reassignment in shared code paths. If another object received the list earlier, that object still sees the old data and may behave as if nothing was cleared.
Developers also sometimes remove items in a forward loop:
That pattern skips items because indexes shift after each removal. If you must remove one by one, go backward or use Clear().
Finally, remember that List<T> is not thread-safe. Clearing a list while another thread enumerates it can throw exceptions or create race conditions. Synchronize access if the list is shared.
Summary
- Use
Clear()as the standard way to empty aList<T>. - Reassigning a variable creates a new list and does not affect other references.
- '
Clear()resetsCount, but capacity may stay allocated for future reuse.' - Only remove items one by one when you need side effects during deletion.
- Be careful with shared references and concurrent access when clearing collections.

