예제 #1
0
 /// <summary>
 /// 从当前数组的指定索引开始截取指定长度的一部分。
 /// 如果 <paramref name="startIndex"/> 小于 <c>0</c>,
 /// 那么表示从字符串结束位置向前计算的位置。
 /// </summary>
 /// <typeparam name="T">数组中元素的类型。</typeparam>
 /// <param name="array">要截取的数组。</param>
 /// <param name="startIndex">要截取的起始索引。</param>
 /// <param name="length">要截取的数组元素个数。</param>
 /// <returns>截取得到的数组。</returns>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="array"/> 为 <c>null</c>。</exception>
 /// <exception cref="System.ArgumentOutOfRangeException">
 /// <paramref name="startIndex"/> 小于负的此数组的长度。</exception>
 /// <exception cref="System.ArgumentOutOfRangeException">
 /// <paramref name="length"/> 小于 <c>0</c>。</exception>
 /// <exception cref="System.ArgumentOutOfRangeException">
 /// <paramref name="startIndex"/> 加 <paramref name="length"/>
 /// 之和指示的位置不在此数组中。</exception>
 public static T[] Subarray <T>(this T[] array, int startIndex, int length)
 {
     if (array == null)
     {
         throw CommonExceptions.ArgumentNull("array");
     }
     if (startIndex < -array.Length)
     {
         throw CommonExceptions.ArgumentOutOfRange("startIndex", startIndex);
     }
     if (length < 0 || startIndex + length > array.Length)
     {
         throw CommonExceptions.InvalidOffsetLength();
     }
     Contract.Ensures(Contract.Result <T[]>() != null);
     if (length == 0)
     {
         return(Empty <T>());
     }
     if (startIndex < 0)
     {
         startIndex += array.Length;
     }
     return(SubarrayInternal(array, startIndex, startIndex + length));
 }
예제 #2
0
        /// <summary>
        /// 从当前数组的指定索引开始截取指定长度的一部分。
        /// 如果 <paramref name="startIndex"/> 小于 <c>0</c>,
        /// 那么表示从字符串结束位置向前计算的位置。
        /// </summary>
        /// <typeparam name="T">数组中元素的类型。</typeparam>
        /// <param name="array">要截取的数组。</param>
        /// <param name="startIndex">要截取的起始索引。</param>
        /// <param name="length">要截取的数组元素个数。</param>
        /// <returns>截取得到的数组。</returns>
        /// <exception cref="ArgumentNullException"><paramref name="array"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> 小于负的此数组的长度。</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> 小于 <c>0</c>。</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> 加 <paramref name="length"/>
        /// 之和指示的位置不在此数组中。</exception>
        public static T[] Subarray <T>(this T[] array, int startIndex, int length)
        {
            CommonExceptions.CheckArgumentNull(array, nameof(array));
            if (startIndex < -array.Length)
            {
                throw CommonExceptions.ArgumentOutOfRange(nameof(startIndex), startIndex);
            }
            if (length < 0 || startIndex + length > array.Length)
            {
                throw CommonExceptions.InvalidOffsetLength();
            }
            Contract.Ensures(Contract.Result <T[]>() != null);
            if (length == 0)
            {
                return(Empty <T>());
            }
            if (startIndex < 0)
            {
                startIndex += array.Length;
            }
            var result = new T[length];

            Array.Copy(array, startIndex, result, 0, length);
            return(result);
        }