title: Can I Limit the depth of a Generic Stack?
body: <p>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?</p>
  
  <p>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.</p>
  
  <p>EDIT: I wrote an extension method:</p>
  
  <pre><code>    public static void Trim&lt;T&gt; (this Stack&lt;T&gt; stack, int trimCount) 
      {
          if (stack.Count &lt;= trimCount)
              return;
  
         stack = new 
              Stack&lt;T&gt;
              (
                  stack
                      .ToArray()
                      .Take(trimCount)
              );           
      }
  </code></pre>
  
  <p>So, it returns a new stack upon trimming, but isn't immutability the functional way =)</p>
  
  <p>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.</p>