JavaScript pairing in C #

Does C # have a similar method for splicefrom JavaScript?

I only know RemoveRange, and this does not return deleted items:

List<string> t = new List<string>();
t.RemoveRange(..., ...);

(I want not to write my own collection).

+4
source share
2 answers

There is no exact equivalence, but you can write one:

public static List<T> Splice<T>(this List<T> source,int index,int count)
{
    var items = source.GetRange(index, count);
    source.RemoveRange(index,count);
    return items;
}
+11
source

If the number of spliced ​​elements is greater than the number of elements in the list, then GetRange () throws an exception. The best decision:

    public static class ListExtension
    {
      public static List<T> Splice<T>(this List<T> source, int start, int size)
      {
          var items = source.Skip(start).Take(size).ToList<T>(); 
          if (source.Count >= size)
              source.RemoveRange(start, size);
          else 
              source.Clear();
          return items;
      }
    }
+1
source

All Articles