title: Splice on collections
body: <p>In perl, the splice function returns a new array of items from an existing array and at the same time removes these items from the existing array.</p>
  
  <pre><code>my @newarry = splice @oldarray, 0, 250;
  </code></pre>
  
  <p><code>@newarray</code> will now contain 250 records from <code>@oldarray</code> and <code>@oldarray</code> is 250 records less.</p>
  
  <p>Is there an equivalent for the C# collection classes ie Array, List, Queue, Stack with a similar function? So far I have only seen solutions where two steps are required (return + remove).</p>
  
  <p>Update - no functionality exists so I have implemented an extensio method to support the Splice function:</p>
  
  <pre><code>public static List&lt;T&gt;Splice&lt;T&gt;(this List&lt;T&gt; Source, int Start, int Size) 
  {
    List&lt;T&gt; retVal = Source.Skip(Start).Take(Size).ToList&lt;T&gt;();
    Source.RemoveRange(Start, Size);
    return retVal;
  }
  </code></pre>
  
  <p>With the following Unit test - which succeeds:</p>
  
  <pre><code>[TestClass]
  public class ListTest
  {
    [TestMethod]
    public void ListsSplice()
    {
      var lst = new List&lt;string&gt;() {
        "one",
        "two",
        "three",
        "four",
        "five"
      };
  
      var newList = lst.Splice(0, 2);
  
      Assert.AreEqual(newList.Count, 2);
      Assert.AreEqual(lst.Count, 3);
  
      Assert.AreEqual(newList[0], "one");
      Assert.AreEqual(newList[1], "two");
  
      Assert.AreEqual(lst[0], "three");
      Assert.AreEqual(lst[1], "four");
      Assert.AreEqual(lst[2], "five");
  
    }
  }
  </code></pre>