Is there a built in way to limit the depth of a System.Collection.Generics.Stack? So that if you are at max capacity, pushing a new element would remove the bottom of the stack?
I know I can do it by converting to an array and rebuilding the stack, but I figured there's probably a method on it already.
EDIT: I wrote an extension method:
public static void Trim<T> (this Stack<T> stack, int trimCount)
{
if (stack.Count <= trimCount)
return;
stack = new
Stack<T>
(
stack
.ToArray()
.Take(trimCount)
);
}
So, it returns a new stack upon trimming, but isn't immutability the functional way =)
The reason for this is that I'm storing undo steps for an app in a stack, and I only want to store a finite amount of steps.