Subset an array - CSharp System

CSharp examples for System:Array Operation

Description

Subset an array

Demo Code


using System;//w  w  w.j a va  2s  . co m

public class Main{
        /// <summary>
        /// Subset an array
        /// </summary>
        /// <typeparam name="T">The type of the input array elements</typeparam>
        /// <param name="array">Input array</param>
        /// <param name="from">Index of the first element to subset</param>
        /// <param name="to">Index of the last element to subset</param>
        /// <returns></returns>
        public static T[] Subset<T>(T[] array, int from, int to)
        {
            if (from > to) throw new ArgumentException("Inconsistent subset: from > to");
            int count = (to - from) + 1;
            var res = new T[count];
            Array.Copy(array, from, res, 0, count);
            return res;
        }
}

Related Tutorials