Remove a range out of the given list and return that range as a new list. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:List

Description

Remove a range out of the given list and return that range as a new list.

Demo Code


using System.Linq;
using System.Collections.Generic;
using System;//from   www.j av a2s  . c o  m

public class Main{
        /// <summary>
        /// Remove a range out of the given list and return that range as a new list.
        /// </summary>
        public static List<T> CutRange<T>(this List<T> list, int start, int count)
        {
            var ret = new List<T>(count);
            for (int i = 0; i < count; i++)
            {
                ret.Add(list[start + i]);
            }
            list.RemoveRange(start, count);
            return ret;
        }
}

Related Tutorials