How to check if a Stack<T> is empty

BobsunShirov picture BobsunShirov · Sep 9, 2014 · Viewed 44.1k times · Source

Is there some other way, except Stack<T>.Count() == 0, to check if a Stack<T> is empty?

Coming from C++/Java background where "stack" classes generally have some sort of dedicated "is empty" method like Java - Stack.empty.

Answer

recursive picture recursive · Sep 9, 2014

Instead of using .Count() == 0, just use .Count == 0. This is using the stack's property rather than the linq extension method.