Friday, October 28, 2022

[FIXED] How to check if a Stack<T> is empty

Issue

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.


Solution

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



Answered By - recursive
Answer Checked By - Mary Flores (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.