In C#, Stack.Clear() removes every item from a Stack<T> in one call. Afterward Count is 0, and the stack is ready to use again.
Imagine sweeping the whole pile of plates off the shelf at once. That is faster than calling Pop() in a loop, and you do not need to read the removed items. The stack object itself stays alive, so you can Push() new items right away.
This is an instance member of Stack<T> (System.Collections.Generic). Call it on a stack variable, such as history.Clear(...). A Stack<T> is a class, so two variables can share one stack. It is last in, first out (LIFO): the newest item sits on top and leaves first. It is not thread-safe; for several threads use ConcurrentStack<T>.
Syntax
Call Clear with no arguments. It returns nothing.
C#
Stack<int> numbers = new Stack<int>();
numbers.Push(1);
numbers.Push(2);
numbers.Clear();
int count = numbers.Count; // 0
Call
What it does
numbers.Clear()
Removes all items. Count becomes 0.
numbers.Clear() (already empty)
Safe. Nothing happens and no exception is thrown.
numbers = new Stack<int>();
Another way to start fresh, but it makes a new stack. Other variables that point to the old stack still see the old items.
numbers.TrimExcess()
After Clear, release the unused storage too. See TrimExcess().
Clear is O(n) for types that hold references, because it must reset the slots so the garbage collector can free those objects. It keeps the stack's capacity (the internal array), so refilling it later is cheap. Calling Clear inside a foreach over the same stack makes the next loop step throw InvalidOperationException, since the stack was changed.
Library declarations
C#
public void Clear();
Parameters
Parameter
Description
None
Clear() takes no parameters.
Return Value
Return Value
Description
void
Nothing is returned. After the call, Count is 0 and Peek() or Pop() would throw until you push items again.
C# Examples
Example 1 — Empty a Stack and Use It Again
Fill a stack, clear it, and check Count. Then push a new item to prove the stack still works.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Stack<string> plates = new Stack<string>();
plates.Push("white");
plates.Push("blue");
plates.Push("red");
plates.Clear();
Console.WriteLine(plates.Count);
Console.WriteLine(plates.Contains("blue"));
plates.Push("green");
Console.WriteLine(plates.Count);
Console.WriteLine(plates.Peek());
}
}
Output
0
False
1
green
How It Works
Three plates in. The stack starts with white at the bottom and red on top.
One call removes them all. After Clear(), Count is 0 and Contains("blue") is False. The pile is bare.
Same object, still works. The variable plates still points to a usable stack. Nothing has to be created again.
A fresh start. Pushing "green" gives a Count of 1, and green is on top because the old plates are no longer there.
Example 2 — Wipe an Undo History After Saving
An editor keeps edits on a stack. After the file is saved there is nothing to undo, so the whole history is cleared. Count the edits first.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Stack<string> edits = new Stack<string>();
edits.Push("type");
edits.Push("bold");
edits.Push("delete");
edits.Push("paste");
int saved = edits.Count;
edits.Clear();
Console.WriteLine("Saved " + saved + " edits");
Console.WriteLine(edits.Count);
}
}
Output
Saved 4 edits
0
How It Works
Four edits recorded. Each action was pushed as it happened, so paste, the latest one, is on top.
Read the size first.Clear() returns nothing, so the code copies Count (which is 4) into savedbefore clearing.
One call, no loop. A single Clear() drops all four edits. No Pop() loop and no throw-away values are needed.
History is empty. The last line prints 0, so nothing can be undone any more. The next edit would start a new history.
Example 3 — Two Variables, One Stack
A stack is a class, so two variables can point to the same object. See how Clear and assigning a new stack behave differently.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Stack<int> original = new Stack<int>();
original.Push(1);
original.Push(2);
Stack<int> alias = original;
alias.Clear();
Console.WriteLine(original.Count);
original.Push(5);
Stack<int> other = original;
original = new Stack<int>();
Console.WriteLine(original.Count);
Console.WriteLine(other.Count);
}
}
Output
0
0
1
How It Works
One stack, two names.alias = original copies the reference, not the items, so both variables control the same stack.
Clear is seen by everyone. Clearing through alias empties the shared stack, so original.Count prints 0.
A new stack is separate.other keeps the old stack (holding the 5), and original = new Stack<int>() points original at a different, empty one.
The result.original.Count is 0 (the new stack) and other.Count is 1 (the old one still has the 5). Clear changes the object; assignment changes only one variable.
Frequently Asked Questions
It removes all items from the stack. After the call Count is 0, and the stack object can be used again.
Not the internal array. Clear keeps the capacity so refilling is cheap. Call TrimExcess if you also want to release the unused storage.
Yes, in most cases, and it is much shorter to write. Use Pop in a loop only when you need to process each item as it leaves.
Clear empties the existing stack object, so every variable that points to it sees the change. new Stack<T>() creates another stack, and the old one stays untouched if something else references it.
Yes. It does nothing and throws no exception, so you do not need a Count check first.